简体   繁体   English

将数组添加为新的键值对

[英]Adding an array as a new key-value pair

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现在看起来像:

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. 我对Java语言还比较陌生,但是我已经为每个公园成功创建了一系列便利设施。 I then tried to add each amenityArray as follows: 然后,我尝试如下添加每个amenityArray:

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? 有人可以演示如何将amenityArray添加为每个公园的键值对吗?

For each object in resultsArray , add the corresponding amenity. 对于resultsArray每个对象,添加相应的便利性。


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: 您可以使用.forEach()遍历数组并根据索引设置各项:

 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: 您可以为此使用map

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM