簡體   English   中英

將數組對象推送到新對象

[英]push array object to new object

我有以下數據:

var data = [{
    County: "Cork",
    date: '1/1/2018',
    num: 100
}, {
    County: "Roscommon",
    date: '07/10/2017',
    num: 27
}];
var Counties = {};
for (County in data) {
    for (var i = 0; i <= 1; i++) {
        Counties[data[i].County] = data[i].County;
    }
}

返回{County: "Roscommon"}

我如何獲取代碼以返回數組對象中的每個縣,以便最終得到:

{County: "Cork"}
{County: "Roscommon"}

您可以使用array#map 您可以使用array#map對其數組進行迭代,並在其回調函數中使用對象分解語法來獲取County值,並使用對象簡寫表示法使用County值創建對象。

 var data = [{ County: "Cork", date:'1/1/2018', num:100 },{ County: "Roscommon", date: '07/10/2017', num:27 }], counties = data.map(({County}) => ({County})); console.log(counties); 

您可以使用array#map函數。

const countyArray = data.map(el => {el.County})

要么

const counties = [];
for (let county of data) {
   counties.push({ County })
}

作為對象:

const counties = {}; 

for (let element of data) { 
counties[element.County] = {County: element.County};
}

你是這個意思嗎 只包含縣值的對象組成的數組?

 var data = [{ County: "Cork", date:'1/1/2018', num:100 },{ County: "Roscommon", date: '07/10/2017', num:27 }]; var Counties=data.map(function(a){return {County:a.County}}); console.log(Counties); 

還是這個,只有縣名的數組?

 var data = [{ County: "Cork", date:'1/1/2018', num:100 },{ County: "Roscommon", date: '07/10/2017', num:27 }]; var Counties=data.map(function(a){return a.County}); console.log(Counties); 

對於數組中的簡單數據映射,建議您深入了解map的功能,這很容易“清理”數組中的數據以選擇所需格式的花絮。

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/map

如果您需要唯一的值,則需要幾個額外的步驟

 var data = [{ County: "Cork", date:'1/1/2018', num:100 },{ County: "Roscommon", date: '07/10/2017', num:27 }, { County: "Roscommon", date: '17/10/2017', num:34 } ]; console.log("#nofilter"); var counties=data.map(function(a){ return a.County }); console.log(counties); console.log("as filtered array"); var counties=data.map(function(a){ return a.County; }) .filter(function(value, index, self) { return self.indexOf(value) === index; }); console.log(counties); console.log("as filtered objects"); var counties=data.map(function(a){return a.County}) .filter(function(value, index, self) { return self.indexOf(value) === index; }) .map(function(a) { return {County: a} }); console.log(counties); 

您的代碼有一些問題,讓我們來解決一下:

您的方法:

var Counties={};
for (County in data){
  for (var i=0;i<=1;i++){
     Counties["County"]=data[i].County;
  }
}

此處沒有很好地使用關鍵字in ,因為該運算符返回Array data每個項目的索引

for (County in data)
            ^

您不需要該內部for-loop因為您可以直接遍歷data數組:-)

for (County in data){
   for (var i=0;i<=1;i++){ // Remove that line
   ^

當您在問題中陳述時,所需的輸出是對象數組。 因此,您需要聲明一個數組。

var Counties={};
^

看下面的代碼片段,它更干凈並遵循您的邏輯:

 var data = [{ County: "Cork", date: '1/1/2018', num: 100 }, { County: "Roscommon", date: '07/10/2017', num: 27 }]; var array = []; for (c in data) { array.push({"County": data[c].County}); } console.log(array); 

看到嗎?,您的邏輯正在起作用!

使用reduce函數的一種簡短方法是:

reduce將不會修改您的原始對象。

 var data = [{ County: "Cork", date: '1/1/2018', num: 100 }, { County: "Roscommon", date: '07/10/2017', num: 27 }]; var counties = data.reduce((a, c) => { return [...a, ...[{"county": c.County}]] }, []); console.log(JSON.stringify(data)); console.log(JSON.stringify(counties)); 

看到嗎?您的原始Obj未被修改,並且創建了帶有縣的新Array。

使用map功能的另一種簡短方法是:

map不會修改您的原始對象。

 var data = [{ County: "Cork", date: '1/1/2018', num: 100 }, { County: "Roscommon", date: '07/10/2017', num: 27 }]; var counties = data.map((c) => ({"county": c.County})); console.log(JSON.stringify(data)); console.log(JSON.stringify(counties)); 

看到嗎?您的原始Obj未被修改,並且創建了帶有縣的新Array。

資源資源

最好的選擇可能是像這樣在數據數組上進行映射。 `

var data = [{
    county: "Cork",
    date:'1/1/2018',
    num:100
  },{
    county: "Roscommon",
    date: '07/10/2017',
    num:27
  }];
var counties=[];
data.map(item => {
var county = {county: item.county};
counties.push(county);
});

`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM