1

I have 2 array of object, just want to check b exist in a, if yes add a new property. but the length of b is dynamic. I will got error of undefined of b.

var a = [{name:'john'},{name:'james'},{name:'jordan'},{name:'joe'}];
var b = [{name:'john'},{name:'joe'}];

var exist = 0;
var c = _.map(a,function(result,i){
    exist = b[i].name.indexOf(a.name) > -1 ? exist = 1 : exist = 0;

    return _.extend({},c,{'exist':exist});
});

any clue?

3 Answers 3

3

I would iterate over b objects and for each of them check if they exists in A, like in the following example.

    var a = [ { _id: '5815adb4badf3f311a2cd25b', username: 'david&jane' },
    { _id: '5815e40e136c8e33b65b3478', username: 'david+jane' } ];
    var b = [ { username: 'david&jane' },
              { username: 'david<3jane' },
              { username: 'david+jane' },
              { username: 'davidjane' } 
    ]
    var c = [];

    b.forEach(function(user) {
      var exists = false;
      for (let i=0; i<a.length && !exists; i++){
          exists = a[i].username === user.username ;
      }
      
      c.push(Object.assign({},user,{exists}));
    });

    console.log(c);

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

8 Comments

what is Object.assign, never seen them before, ES6?
Yea, suppose to act like your _.extend({},c,{'exist':exist});
assume my a has extra id field, then I can't apply ur solution, mind to take a look why? pastebin.com/1PTPZpkc
instead of use Object.assign, can use lodash? too risky to use ES6 for old browser.
@ThianKianPhin I updated the example with the objects with the id property and I can't see a problem. And yea I dont see why you wont be able to use loadash
|
1

First stringify a array. Loop through b and create a dynamic regex to test on a if the test matches do your stuff.

Hope this helps!

var a = [{name:'john'},{name:'james'},{name:'jordan'},{name:'joe'}];
var b = [{name:'john'},{name:'joe'}, {name:'hello'}];

var strA = JSON.stringify(a)

var result = b.map((el) => {
  var elStr = JSON.stringify(el)
  var regex = new RegExp(elStr, 'g')
  if(regex.test(strA))
     return Object.assign({}, el, {exist: 1})
  return Object.assign({}, el, {exist: 0})
})

console.log(result)

3 Comments

I did not downvote but this seems too complicated for me.
It is the most simplest answer with one loop.
You just need to loop through b. The other answer will loop through a for each element in b which is really costly.
1

You could use Array.every to check that every object in b is also in a, and in the callback you could use Array.some to check a for keys and values

var a = [{name:'john'},{name:'james'},{name:'jordan'},{name:'joe'}];
var b = [{name:'john'},{name:'joe'}];

var exist = b.every( (o) => {
    let k = Object.keys(o)[0];
    return a.some( p => k in p && p[k] === o[k]);
});

console.log(exist)

If you want to add properties, you can do that as well

var a = [{name:'john'},{name:'james'},{name:'jordan'},{name:'joe'}];
var b = [{name:'john'},{name:'joe'}];

var c = b.map( (o) => {
    let k = Object.keys(o)[0];
    return o.exist = a.some( p => k in p && p[k] === o[k]), o;
});

console.log(c);

2 Comments

Hey, the first example should return true twice isn't it?
Well, no, I just made the first example return true or false based on wether or not all the objects in b are in a.

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.