I have an existing array which contains key-value pairs for a series of items:
resultsArray = [
{
parkName: "Central Park",
owner: "NYC"
},
{
parkName: "Patterson Park",
owner: "Baltimore"
}
]
I'd like to add an additional key-value pair to each item where the value is an array of amenities. For example: if Central Park includes a dog park, jogging trail, and basketball courts, and Patterson Park includes a baseball field and a rec center, when the amenities were added to the array, the resultsArray would now look like:
resultsArray = [
{
parkName: "Central Park",
owner: "NYC",
amenities: "Dog Park", "Jogging Trail", "Basketball Courts"
},
{
parkName: "Patterson Park",
owner: "Baltimore",
amenities: "Baseball Fields", "Recreation Center"
}
]
I'm relatively new to Javascript, but I've successfully created an array of amenities for each of the parks. I then tried to add each amenityArray as follows:
resultsArray.push({
amenities: amenityArray
});
However, this didn't give me what I wanted.
Can someone demonstrate how I would add the amenityArray as a key-value pair for each of the parks?
For each object in resultsArray
, add the corresponding amenity.
for (let i = 0; i < resultsArray.length; i++) {
resultsArray[i][i]['amenities'] = amenityArray[i];
}
A more verbose version would be
for (let i = 0; i < resultsArray.length; i++) {
let result = resultsArray[i]; // i is an index
let innerResult = result[i]; // i is a key
innerResult['amenities'] = amenityArray[i];
}
Iterate the array and add a new key-value pair in the inner objects.
Note: Picked the amenities dynamically.
resultsArray = [{ 0: { parkName: "Central Park", owner: "NYC" } }, { 1: { parkName: "Patterson Park", owner: "Baltimore" } }]; amenities = { "0": "X Park, Y Park, Z Park", "1": "A Park, B park, C Park" }; [...resultsArray].forEach(token => { Object.keys(token).forEach(key => token[key]['amenities'] = amenities[key]); }) console.log(resultsArray);
You can use .forEach()
to loop through the array and set each item depend on index:
var resultsArray = [{parkName: "Central Park",owner: "NYC"},{parkName: "Patterson Park", owner: "Baltimore"}] var amenityArray = [["Dog Park", "Jogging Trail", "Basketball Courts"], ["Baseball Fields", "Recreation Center"]]; resultsArray.forEach((val, index) => {val.amenities = amenityArray[index]}) console.log(resultsArray)
You can use map
for this:
const newArray = resultsArray.map((item, i) => {
item[i].amenities = amenityArray[i];
return item;
});
Also your array structure looks weird, you should consider removing those redundant nested objects, and then it will be easier to iterate:
resultsArray = [{
parkName: "Central Park",
owner: "NYC"
},
{
parkName: "Patterson Park",
owner: "Baltimore"
}
]
const newArray = resultsArray.map((item, i) => {
item.amenities = amenityArray[i];
return item;
});
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.