Reputation: 53
I've tried to find an answer but was not successful.
The sample of json data, lets say it would be named mainArray:
[{
"id": 4455,
"key1": 44,
"key2": 55
},
{
"id": 1122,
"key1": 11,
"key2": 22
}]
The sample of data nestedArray needs to be added in mainArray:
[{
"nestedkey0": 'Value0',
"nestedkey1": "Value1",
"nestedkey2": "Value2"
},
{
"nestedkey0": "Value3",
"nestedkey1": "Value4",
"nestedkey2": "Value5"
}]
And we have to check if "id" key (in mainArray) equals "specificId" variable:
var specificId = 4455;
In case they are equal — we need to add nestedArray into the object of mainArray as a new key with list of objects, so returned result should be like this:
[{
"id": 4455,
"newKey": [{
"nestedkey0": 'Value0',
"nestedkey1": "Value1",
"nestedkey2": "Value2"
},
{
"nestedkey0": "Value3",
"nestedkey1": "Value4",
"nestedkey2": "Value5"
}];
"key1": 44,
"key2": 55
},
{
"id": 1122,
"key1": 11,
"key2": 22
}];
Here's a sample of code on Plunker: https://plnkr.co/edit/YJ3lTbleKrBJBaJm7VwU?p=preview
I've got a feeling that shouldn't be hard, but I'm not quite experienced in angularjs to resolve this task. Thank you in advance for your answers!
Update
Updated code in Plunker with answer of Nina Scholz (link is above)
And another solution from Halcyon
http://jsbin.com/xaxagusuma/edit?html,js,console,output
Thanks to all for your help!
Upvotes: 3
Views: 94
Reputation: 541
What about keeping it simple :)
for (var i = 0, i < mainArray.length; i++) {
if (mainArray[i].id === specificId) {
mainArray[i].newKey = nestedArray;
break;
}
}
Will work in all browsers.
Upvotes: 1
Reputation: 57719
As you suspect this isn't very hard. The hardest part is finding the correct object to insert the nastedArray.
var mainArray = [/**/];
var nastedArray = [/***/];
var index = null;
var specificId = 4455;
for (var i = 0; index === null, i < mainArray.length; i += 1) {
if (mainArray[i].id === specificId) {
index = i;
}
}
if (index !== null) {
mainArray[index].newKey = nastedArray;
}
Upvotes: 1
Reputation: 21881
An alternative with Array.prototype.find
The find() method returns a value in the array, if an element in the array satisfies the provided testing function. Otherwise undefined is returned.
(have a look at the compatibility table!)
var mainArray = [{ "id": 4455, "key1": 44, "key2": 55 }, { "id": 1122, "key1": 11, "key2": 22 }],
nastedArray = [{ "nastedkey0": 'Value0', "nastedkey1": "Value1", "nastedkey2": "Value2" }, { "nastedkey0": "Value3", "nastedkey1": "Value4", "nastedkey2": "Value5" }],
specificId = 4455;
var item = mainArray.find(x => x.id === specificId);
if (item !== undefined) {
item.newKey = nastedArray;
}
console.log(JSON.stringify(mainArray));
Upvotes: 1
Reputation: 386604
In plain Javascript, you can use Array.prototype.some()
for iterating, because of a possible short-circuiting.
The
some()
method tests whether some element in the array passes the test implemented by the provided function.
var mainArray = [{ "id": 4455, "key1": 44, "key2": 55 }, { "id": 1122, "key1": 11, "key2": 22 }],
nastedArray = [{ "nastedkey0": 'Value0', "nastedkey1": "Value1", "nastedkey2": "Value2" }, { "nastedkey0": "Value3", "nastedkey1": "Value4", "nastedkey2": "Value5" }],
specificId = 4455;
mainArray.some(function (a) {
if (a.id === specificId) {
a.newKey = nastedArray;
return true;
}
});
document.write('<pre>' + JSON.stringify(mainArray, 0, 4) + '</pre>');
Upvotes: 3