简体   繁体   中英

how to add property to property to an object based on the condition in javascript?

i have 2 arrays as show below:

arr = [
  { "zc": 33004, "score": 32.61 },
  { "zc": 33005, "score": 88.51 },
  ...
]

arr2 = [
  "type": "feature", properties: { "zc": 33004 }, geometry: { ... },
  "type": "feature", properties: { "zc": 33005}, geometry: { ... }, 
  "type": "feature", properties: { "zc": 33009}, geometry: { ... }
]

expected result = [
  "type": "feature", properties: { "zc": 33004, "score": 32.61 }, geometry: { ... },
  "type": "feature", properties: { "zc": 33005, "score": 88.51 }, geometry: { ... }, 
  "type": "feature", properties: { "zc": 33009, "score": 0 }, geometry: { ... }
]

Here, i want to add the score from the first array if the second array zc is matched inside properties object in each array of objects.

am writing a piece of code using spread operator as shown below

  arr.forEach(ele => {
      arr2.forEach(element => {
        element = {
          ...element,
          ...((ele.zipcode==element.properties.zipcode) ? {element.properties.scope: ele.zipcode} : {element.properties.scope: 0})
        }
      });
    }) 
  console.log(arr2);

but am getting compile time error. where am i doing wrong?

You can make a temp object for arr using reduce . Use the zc as key and the score as the value. This will make easier to check if zc exist.

Use map to loop thru the arr2

 let arr = [{"zc":33004,"score":32.61},{"zc":33005,"score":88.51}] let arr2 = [{"type":"feature","properties":{"zc":33004},"geometry":{}},{"type":"feature","properties":{"zc":33005},"geometry":{}},{"type":"feature","properties":{"zc":33009},"geometry":{}}] let tempArr = arr.reduce((c, v) => Object.assign(c, {[v.zc]: v.score}), {}) let result = arr2.map(o => { o = {...o} //shallow copy the object o.properties = {...o.properties,score: tempArr[o.properties.zc] || 0} return o; }) console.log(result); 

You get a compile error because you cannot use the dot inside an object propery name (like { element.properties.scope: ... } )., So, you should do this as follows:

arr.forEach(arrElem =>
  arr2.forEach(arr2Elem =>
    arr2Elem.properties = {
      ...arr2Elem.properties,
      ...{
        score: arrElem.zipcode === arr2Elem.properties.zipcode ? arrElem.score : 0
      }
    }
  );
);

console.log(arr2);

But, I think this isn't the right way to do it. I think you should use find() , as the following:

arr2.forEach(arr2Elem =>
  arr2Elem.properties = {
    ...arr2Elem.properties,
    ...{
      score: (arr.find(arrElem => arrElem.zipcode === arr2Elem.properties.zipcode) || { score: 0 }).score
    }
  }
);

console.log(arr2);

(I don't change the arr2Elem directly, but I change its property properties beacuse the spread operator cannot use with subobjects).

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.

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