如何在javascript中使用特定键在对象数组中添加另一个对象

时间:2017-11-29 09:10:16

标签: javascript arrays node.js object

我有一个包含具有受尊重键值的对象属性的数组。我需要在数组中附加一个具有相同键的对象。

我的数组是:

var sam = [
  { Absenteeism: [{
  "SubIndicator": "Absenteeism (PH Worker / Supervisor)",
  "Target": "",
  "Actual": "",
  "Variance": "",
  "Benchmark": 100,
  "Weightage": 25,
  "PenalitySubIndicator": "",
  "PenalityKeyIndicator": ""
  }] },
  {Coverage: [{"KeyIndicator":"Coverage",
  "SubIndicator":"Door to Door (GatesCovered)",
  "Target":null,"Actual":null,
  "Variance":null,
  "Benchmark":100,"Weightage":25,
  "PenalitySubIndicator":null,
  "PenalityKeyIndicator":null,
  "ScorecardId":1}  ] },    
  { Weighment: [    {"KeyIndicator":"Weighment",
  "SubIndicator":"Dry",
   "Target":null, 
  "Actual":null,
  "Variance":null,
   "Benchmark":80,
  "Weightage":25,
   "PenalitySubIndicator":null, 
   "PenalityKeyIndicator":null,
  "ScorecardId":1}  ] }, 
   { Weighment: [  {"KeyIndicator":"Weighment",
  "SubIndicator":"Wet",
  "Target":null,
  "Actual":null,
  "Variance":null,
  "Benchmark":80,
  "Weightage":25,
  "PenalitySubIndicator":null,
  "PenalityKeyIndicator":null,
  "ScorecardId":1}  ] },
  { Transportation: [ {"KeyIndicator":"Transportation",
  "SubIndicator":"Punctuality",
  "Target":null,"Actual":null,
  "Variance":null,
  "Benchmark":100,
  "Weightage":25,
  "PenalitySubIndicator":null,
  "PenalityKeyIndicator":null,
  "ScorecardId":1}  ] },  
  { Transportation: [  {"KeyIndicator":"Transportation",
  "SubIndicator":"Transfer Point Coverage",
  "Target":null,
   "Actual":null,
  "Variance":null,
  "Benchmark":100,
  "Weightage":25,
  "PenalitySubIndicator":null,
  "PenalityKeyIndicator":null,
  "ScorecardId":1}  ] } 
]

预期的输出数组是

var sam =  [{
  "Absenteeism": [{
    "SubIndicator": "Absenteeism (PH Worker / Supervisor)",
    "Target": "",
    "Actual": "",
    "Variance": "",
    "Benchmark": 100,
    "Weightage": 25,
    "PenalitySubIndicator": "",
    "PenalityKeyIndicator": ""
  }],
  "Coverage": [{

    "SubIndicator": "Door to Door (Gates Covered)",
    "Target": "",
    "Actual": "",
    "Variance": "",
    "Benchmark": 100,
    "Weightage": 25,
    "PenalitySubIndicator": "",
    "PenalityKeyIndicator": ""
  }],
  "Weighment": [{

      "SubIndicator": "Dry",
      "Target": "",
      "Actual": "",
      "Variance": "",
      "Benchmark": 80,
      "Weightage": 25,
      "PenalitySubIndicator": "",
      "PenalityKeyIndicator": ""
    },
    {

      "SubIndicator": "Wet",
      "Target": "",
      "Actual": "",
      "Variance": "",
      "Benchmark": 80,
      "Weightage": 25,
      "PenalitySubIndicator": "",
      "PenalityKeyIndicator": ""
    }
  ],
  "Transportation": [{

      "SubIndicator": "Punctuality",
      "Target": "",
      "Actual": "",
      "Variance": "",
      "Benchmark": 100,
      "Weightage": 25,
      "PenalitySubIndicator": "",
      "PenalityKeyIndicator": ""

    },
    {
      "SubIndicator": "Transfer Point Coverage",
      "Target": "",
      "Actual": "",
      "Variance": "",
      "Benchmark": 100,
      "Weightage": 25,
      "PenalitySubIndicator": "",
      "PenalityKeyIndicator": ""

    }
  ]
}]

现在,我需要在单个阵列中获得称重(湿和干),并且与运输(准时和转移点覆盖)相同。所有这些都应该是动态的表示法。

1 个答案:

答案 0 :(得分:0)



var sam = [{ Absenteeism: [{
"SubIndicator": "Absenteeism (PH Worker / Supervisor)",
"Target": "",
"Actual": "",
"Variance": "",
"Benchmark": 100,
"Weightage": 25,
"PenalitySubIndicator": "",
"PenalityKeyIndicator": ""
}] },
{Coverage: [{"KeyIndicator":"Coverage",
"SubIndicator":"Door to Door (GatesCovered)",
"Target":null,"Actual":null,
"Variance":null,
"Benchmark":100,"Weightage":25,
"PenalitySubIndicator":null,
"PenalityKeyIndicator":null,
"ScorecardId":1}  ] },    
{ Weighment: [    {"KeyIndicator":"Weighment",
"SubIndicator":"Dry",
 "Target":null, 
"Actual":null,
"Variance":null,
 "Benchmark":80,
"Weightage":25,
 "PenalitySubIndicator":null, 
 "PenalityKeyIndicator":null,
"ScorecardId":1}  ] }, 
 { Weighment: [  {"KeyIndicator":"Weighment",
"SubIndicator":"Wet",
"Target":null,
"Actual":null,
"Variance":null,
"Benchmark":80,
"Weightage":25,
"PenalitySubIndicator":null,
"PenalityKeyIndicator":null,
"ScorecardId":1}  ] },
{ Transportation: [ {"KeyIndicator":"Transportation",
"SubIndicator":"Punctuality",
"Target":null,"Actual":null,
"Variance":null,
"Benchmark":100,
"Weightage":25,
"PenalitySubIndicator":null,
"PenalityKeyIndicator":null,
"ScorecardId":1}  ] },  
{ Transportation: [  {"KeyIndicator":"Transportation",
"SubIndicator":"Transfer Point Coverage",
"Target":null,
 "Actual":null,
"Variance":null,
"Benchmark":100,
"Weightage":25,
"PenalitySubIndicator":null,
"PenalityKeyIndicator":null,
"ScorecardId":1}  ] } ]
var resultArray = sam.reduce((accumulator, current) => {
     console.log(current, accumulator)
     Object.keys(current).forEach((element) => {
       accumulator.push(current[element])
     })
     return accumulator
   }, [])
 console.log(resultArray)




var resultArray = sam.reduce((accumulator, current) => {
     Object.keys(current).forEach((element) => {
       accumulator.push(current.element)
     })
     return accumulator
   }, [])

// ES6方式