0

I need one help. I need to insert one new value into existing array by matching the key value using Javascript.I am explaining the scenario below.

var galArr=[
     {'image':'12.png','comment':'hii','act':'edit'},
     {'image':'13.png','comment':'hello','act':'edit'},
     {'image':'14.png','comment':'hee','act':'edit'},
]

The above is my existing array.I need to match with the below another array.

var arr=[
    {'image':'12.png','comment':'hii'},
    {'image':'14.png','comment':'hee'},
]

Here i need to match the array arr with an array galArr if image name will same this checked:true will add in the rective row of existing array galArr. Suppose arr[0].image==galArr[0].image then checked:true will add in that respective row of existing array. Please help me.

1
  • I would check my answer and the updated I did some minutes ago, because your selected answer may hurt performance if our gallery array contains a lot of elements. Check the set part. Commented Sep 29, 2016 at 9:59

3 Answers 3

1

This should be sufficient.

var galArr=[
     {'image':'12.png','comment':'hii','act':'edit'},
     {'image':'13.png','comment':'hello','act':'edit'},
     {'image':'14.png','comment':'hee','act':'edit'},
];
var arr=[
    {'image':'12.png','comment':'hii'},
    {'image':'14.png','comment':'hee'},
];

// start looping over `arr`
arr.forEach(function(o, i){
   
  // now loop over `galArr` to find match
  galArr.forEach(function(gO, i){
      // when there is a match
      if(o.image == gO.image){
         console.log(gO);
         // add checked property to this object
         gO['checked'] = true;
      }
  });
  
});

// Output
console.log(galArr);

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

Comments

0

First of all check condition and if the condition match then create a new temp json and replace it with old json

 arr.forEach(function(d){
       galArr.forEach(function(e){
            if(e.image==d.image){
               temp = {};
               temp.image = e.image;
               temp.comment = e.comment;
               temp.checked = e.comment;
               temp.action = e.action;
               e = temp;
            }
       });
    });

1 Comment

You should add an explanation as to what your code is doing - adding just code is not a good way to answer a question
0

I would create an image index, where its indexes would be the whole image file names and later I would use that image index to quickly check and add checked property to galArr array:

var galArr=[
     {'image':'12.png','comment':'hii','act':'edit'},
     {'image':'13.png','comment':'hello','act':'edit'},
     {'image':'14.png','comment':'hee','act':'edit'},
];
  
var imageIndex = galArr.map(function(item) {
    return item.image;
});

var arr=[
    {'image':'12.png','comment':'hii'},
    {'image':'14.png','comment':'hee'},
]

arr.forEach(function(item) {
    item.checked = imageIndex.indexOf(item.image) > -1;
});

If your users will use your JavaScript code within a modern Web browser, I would use the new Set collection:

var galArr=[
     {'image':'12.png','comment':'hii','act':'edit'},
     {'image':'13.png','comment':'hello','act':'edit'},
     {'image':'14.png','comment':'hee','act':'edit'},
];
  
var imageIndex = galArr.reduce(function(result, item) {
    result.add(item.image);
 
    return result;
}, new Set());

var arr=[
    {'image':'12.png','comment':'hii'},
    {'image':'14.png','comment':'hee'},
]

arr.forEach(function(item) {
    item.checked = imageIndex.has(item.image);
});

I've asked a question to assist everyone in understanding how valueable are sets: Is Set a hashed collection in JavaScript?

Comments

Your Answer

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