我如何在这个庞大的对象列表中为每个对象添加额外的键和值

时间:2019-03-13 03:50:03

标签: javascript arrays json object

我遇到以下问题,我需要向此列表中的每个对象添加一个新的键和值对,

module.exports = [
{"A": "P17060024503", "B": "6/14/2017 21:54", "C": "4", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1151", "J": "O", "K": "521", "L": "2", "M": "32.7054489", "N": "-117.1518696"},
{ "A": "P17030051227", "B": "3/29/2017 22:24", "C": "4", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1016", "J": "A", "K": "521", "L": "2", "M": "32.7054544", "N": "-117.1467137"},
{ "A": "P17060004814", "B": "6/3/2017 18:04", "C": "7", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1016", "J": "A", "K": "521", "L": "2", "M": "32.7053961", "N": "-117.1444185"},
{ "A": "P17030029336", "B": "3/17/2017 10:57", "C": "6", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1151", "J": "OT", "K": "521", "L": "2", "M": "32.7054244", "N": "-117.1425917"},
{ "A": "P17030005412", "B": "3/3/2017 23:45", "C": "6", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "911P", "J": "CAN", "K": "521", "L": "2", "M": "32.7055067", "N": "-117.1405936"},
{ "A": "P17020016091", "B": "2/10/2017 8:23", "C": "6", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "AU2", "J": "W", "K": "521", "L": "2", "M": "32.7055067", "N": "-117.1405936"},
{ "A": "P17040017368", "B": "4/11/2017 4:57", "C": "3", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "5150", "J": "CAN", "K": "521", "L": "2", "M": "32.7055067", "N": "-117.1405936"},
{ "A": "P17030048050", "B": "3/28/2017 6:30", "C": "3", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "1146", "J": "K", "K": "521", "L": "", "M": "32.711212", "N": "-117.1336713"},
{ "A": "P17060037341", "B": "6/22/2017 10:19", "C": "5", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "242", "J": "K", "K": "521", "L": "1", "M": "32.7058661", "N": "-117.132085"},
{ "A": "P17060008467", "B": "6/5/2017 19:27", "C": "2", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "5150", "J": "K", "K": "521", "L": "2", "M": "32.7093133", "N": "-117.12756"},

我要添加“ O”: O是我从上方指向每个对象的关键,而我的值是该列表ARRIVE ON SCENE PRISONER IN CUSTODY PTU (PRISONER TRANSPORT) SHOPLIFTER/QUICK CITE BORDERPRISONERTRANSPORT RETURN TO STATION PHONE YOUR STATION PHONE YOUR HOME SECURE, END OF SHIFT AUTO THEFT EVAL-POSS UNREPORTED CAR THEFT CAR THEFT REPORT CAR THEFT RECOVERY REPORT TAMPERING W/VEH IN PROGRESS VEHICLE ALARM/AUDIBLE VEHICLE CASER TAMPERING W/VEH REPORT MEET OFFICER 10-10 DISCHARGING FIREARMS PROWLER PERSON DOWN REPORT OF CRIME OCCD MEXICO CHECK THE AREA BALL GAME IN STREET(83 03MC) INCOMPLETE PHONE CALL CALLING FOR HELP CALLING FOR HELP-PANIC ALARM AMBULANCE CALL, OVERDOSE

中的任何一个

理想地看起来像这样

 "O" : "PRISONER IN CUSTODY"},
... "O": "Shoplifter/Quick Cite"},
.... "O": "Return TO Station"}, etc

4 个答案:

答案 0 :(得分:1)

您可以将新列表存储在数组中

oValues=[
'ARRIVE ON SCENE',
'PRISONER IN CUSTODY',
'PTU (PRISONER TRANSPORT)'
.
.
.]

然后将oValues的值分配给如下所示的主列表

module.exports.forEach((item,index)=>{item['O']=oValues[index]})

假设您必须分配'O'值序列

答案 1 :(得分:0)

执行此操作。真的有什么问题吗?

var arr = [/*your array*/];
function getRandomO() {/*returns your random O*/}
arr.forEach(v=>v.O = getRandomO());
module.exports = arr;

如果您使用JSON,请在控制台中执行JSON.stringify(arr)并复制过去的结果。

答案 2 :(得分:0)

您可以使用.forEach循环将O属性添加到导出中的每个对象。这将是线性(O(N)),因为您只需迭代对象一次。

const crimes = ["ARRIVE ON SCENE", "PRISONER IN CUSTODY", "PTU (PRISONER TRANSPORT)", "SHOPLIFTER/QUICK CITE", "BORDERPRISONERTRANSPORT", "RETURN TO STATION", "PHONE YOUR STATION", "PHONE YOUR HOME", "SECURE, END OF SHIFT", "AUTO THEFT", "EVAL-POSS UNREPORTED CAR THEFT", "CAR THEFT REPORT", "CAR THEFT RECOVERY REPORT", "TAMPERING W/VEH IN PROGRESS", "VEHICLE ALARM/AUDIBLE", "VEHICLE CASER", "TAMPERING W/VEH REPORT", "MEET OFFICER 10-10", "DISCHARGING FIREARMS", "PROWLER", "PERSON DOWN", "REPORT OF CRIME OCCD MEXICO", "CHECK THE AREA", "BALL GAME IN STREET(83 03MC)", "INCOMPLETE PHONE CALL", "CALLING FOR HELP", "CALLING FOR HELP-PANIC ALARM", "AMBULANCE CALL, OVERDOSE"];
const exports = [{A:"P17060024503",B:"6/14/2017 21:54",C:"4",D:"10",E:"",F:"14TH",G:"ST",H:"10 14TH ST, San Diego, CA",I:"1151",J:"O",K:"521",L:"2",M:"32.7054489",N:"-117.1518696"},{A:"P17030051227",B:"3/29/2017 22:24",C:"4",D:"10",E:"",F:"14TH",G:"ST",H:"10 14TH ST, San Diego, CA",I:"1016",J:"A",K:"521",L:"2",M:"32.7054544",N:"-117.1467137"},{A:"P17060004814",B:"6/3/2017 18:04",C:"7",D:"10",E:"",F:"14TH",G:"ST",H:"10 14TH ST, San Diego, CA",I:"1016",J:"A",K:"521",L:"2",M:"32.7053961",N:"-117.1444185"},{A:"P17030029336",B:"3/17/2017 10:57",C:"6",D:"10",E:"",F:"14TH",G:"ST",H:"10 14TH ST, San Diego, CA",I:"1151",J:"OT",K:"521",L:"2",M:"32.7054244",N:"-117.1425917"},{A:"P17030005412",B:"3/3/2017 23:45",C:"6",D:"10",E:"",F:"15TH",G:"ST",H:"10 15TH ST, San Diego, CA",I:"911P",J:"CAN",K:"521",L:"2",M:"32.7055067",N:"-117.1405936"},{A:"P17020016091",B:"2/10/2017 8:23",C:"6",D:"10",E:"",F:"15TH",G:"ST",H:"10 15TH ST, San Diego, CA",I:"AU2",J:"W",K:"521",L:"2",M:"32.7055067",N:"-117.1405936"},{A:"P17040017368",B:"4/11/2017 4:57",C:"3",D:"10",E:"",F:"15TH",G:"ST",H:"10 15TH ST, San Diego, CA",I:"5150",J:"CAN",K:"521",L:"2",M:"32.7055067",N:"-117.1405936"},{A:"P17030048050",B:"3/28/2017 6:30",C:"3",D:"10",E:"",F:"15TH",G:"ST",H:"10 15TH ST, San Diego, CA",I:"1146",J:"K",K:"521",L:"",M:"32.711212",N:"-117.1336713"},{A:"P17060037341",B:"6/22/2017 10:19",C:"5",D:"10",E:"",F:"15TH",G:"ST",H:"10 15TH ST, San Diego, CA",I:"242",J:"K",K:"521",L:"1",M:"32.7058661",N:"-117.132085"},{A:"P17060008467",B:"6/5/2017 19:27",C:"2",D:"10",E:"",F:"15TH",G:"ST",H:"10 15TH ST, San Diego, CA",I:"5150",J:"K",K:"521",L:"2",M:"32.7093133",N:"-117.12756"}];

exports.forEach(obj => {
  const randomIndex = Math.floor(Math.random() * crimes.length);
  obj["O"] = crimes[randomIndex];
});

console.log(exports);
.as-console-wrapper {max-height: 100% !important; top: 0;} /* ignore css */

答案 3 :(得分:0)

将解决方案分为关联数组或字典列表

let list = [{},{},{} ... ]

访问此数组的第n个元素:list [n] 因此,如果您希望在索引n处添加新的键/值对:

list[n]['key'] = value


let text = ['PRISONER IN CUSTODY', 'PTU (PRISONER TRANSPORT)' ... ]
for(t in text) {
   let key = 'O'
   let value = text[t]
   list[t][key] = value
}