2

I have an array of object, this array contains differents names.

[ "Foo", "Bar", "Test", "Other" ];

And another array of object

[ { name:"Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 } ]

I need to create an array of them, the new array contains sub array with a size of 2, first index the name and second index the value. The order of the array need to be the same of the first one (array of names).

Like

[ ["Foo", 120], ["Bar", 159], ["Test", undefined], ["Other", 1230] ]

So I try this code, but my output is not correct. The order of name is correct but the order of value is not.

var order = ["Foo", "Bar", "Test", "Other"];
var values = [{ name: "Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 }];

var array = order.map(function(name, i) {
  return [name, (values[i] && values[i].value) ];
})

console.log(array)

6 Answers 6

5

You could take a Map and get the items in the wanted order.

var names = [ "Foo", "Bar", "Test", "Other" ],
    objects = [{ name: "Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 }],
    map = new Map(objects.map(({ name, value }) => [name, value])),
    result = names.map(name => [name, map.get(name)])

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

5

You're nearly there - you just need to find the correct value:

var order = ["Foo", "Bar", "Test", "Other"];
var values = [{ name: "Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 }];

var array = order.map(function(name, i) {
  return [name, values.some(e => e.name == name) ? values.find(e => e.name == name).value : undefined];
})

console.log(array)

ES5 syntax:

var order = ["Foo", "Bar", "Test", "Other"];
var values = [{ name: "Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 }];

var array = order.map(function(name, i) {
  return [name, values.some(function(e) { 
    return e.name == name;
  }) ? values.find(function(e) {
    return e.name == name;
  }).value : undefined];
});

console.log(array)

Comments

4

Check out my solution. Hope this helps.

const a = [ "Foo", "Bar", "Test", "Other" ];
const b = [ { name:"Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 } ];

const res = a.map((item) => [ item, (b.find(({ name }) => name === item) || {}).value ])

console.log(res)

1 Comment

Note that name === item is invoked len(a) x len(b) times - this might or might not matter, depending on how large they are.
4

You need to use entries in "order" to lookup "name" in values

var order = ["Foo", "Bar", "Test", "Other"];
var values = [{ name: "Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 }];

var array = order.map(function(key, i) {
  let found = values.find(({name}) => name === key);
  return [key, found && found.value ];
})

console.log(array)

Comments

3

You could use map and take the Object.values of the found item, or a default one

const arr1 = [ "Foo", "Bar", "Test", "Other" ];

const arr2 = [ { name:"Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 } ];

const res = arr1.map(e => Object.values(arr2.find(({name}) => name === e) || ({name: e, value: undefined})));
console.log(res);

Comments

1
var order = ["Foo", "Bar", "Test", "Other"];
var values = [{ name: "Bar", value: 159 }, { name: "Foo", value: 120 }, { name: "Other", value: 1230 }];
var result = [];
orders.forEach((f) => {
let found = false;

values.forEach(s => {
    if (f === s.name) {
result.push([f, s.value]);
        found = true;
}
});
if (!found) {
    result.push([f, undefined]);
}
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.