0

I'm given the following data:

Reference List:

[{
    name: "dog", present: false
}, {
    name: "cat", present: true
}, {
    name: "bird", present: false
}]

Given List:

["dog, cat"]

Wanted Result:

[{
    name: "dog", present: true
}, {
    name: "cat", present: true
}, {
    name: "bird", present: false
}]

Right now I can produce the Wanted Result by creating 3 conditional statements and performing an indexOf. However, I'm wondering if there's a way to do that all in 1 line through something like lodash.

1
  • yes using indexOf, and run a for from referenced to update present value Commented Feb 7, 2017 at 8:15

6 Answers 6

3

Is that what you need?

let reference = [{name:"dog", present:false}, {name:"cat", present:true}, {name:"bird", present:false }];
let list = ['dog', 'cat'];

let result = reference.map(item => ({
  name: item.name,
  present: list.indexOf(item.name) !== -1
}));

console.log(result);

It is logically doing the same you wrote, just utilizing the .map function for that.

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

Comments

1

Shortest solution using Array.prototype.forEach() and Array.prototype.indexOf() functions:

var data = [{ name: "dog", present: false }, { name: "cat", present: true }, { name: "bird", present: false }],
    f = ["dog", "cat"];

data.forEach(function (o) { o.present = (f.indexOf(o.name) !== -1); });
console.log(data);

Comments

0

You can use Array.map()

var reflist = [{
  name: "dog",
  present: false
}, {
  name: "cat",
  present: true
}, {
  name: "bird",
  present: false
}];

var givenList = ["dog", "cat"];

reflist = reflist.map(function(elem){
  if(givenList.indexOf(elem.name) !== -1)
    elem.present = true; 
  return elem;
});

console.log(reflist);

Comments

0

Just another way to do it.

var fields = [{
  name: "dog", present: false
}, {
  name: "cat", present: true
}, {
  name: "bird", present: false
}];

console.log(fields);

['dog', 'cat'].forEach(function (value) {
  fields.forEach(function (k) {
    if (k.name === value) {
      k.present = true;
    }
  });
});

console.log(fields);

Comments

0

You'll need to loop the items of the ref data, compare each with the given set of items and build a new array of objects.

var wantedResult = [];
var keys = Object.keys(refData);
for(var i=0; i<keys.length; i++) {
    wantedResult.push({
        name:refData[keys[i]].name,         
        present:givenList.indexOf(refData[keys[i]].name) != -1
    });
}

Comments

0

Here is your one-liner:

referenceList.map((e) => { e.present = givenList.indexOf(e.name) > -1;})

Note that you'll modify referenceList and this line's return value will have no use for you. It will be just a list of undefined

Reference

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.