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.
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);
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;
}
});
});
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?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.