[英]How to add a key value pair to an array of objects in javascript with multiple value?
[英]How to add a key value pair to an array in Javascript / Jquery
我有2個數組如下
var dataSource = [
{location: "France", January: 79, February: 81, March: 23},
{location: "Germany", January: 98, February: 83},
{location: "Japan", January: 96, March: 11} ];
var Months = ["January","February","March"];
我想通過循環中的每個對象dataSource
和檢查的所有值Months
在每個對象都存在dataSource
。 如果dataSource
中不存在該值,則將該值添加到dataSource
,其值為100
例如:在德國的位置,月份“三月”不存在所以我需要按下鍵和值March : 100
最后dataSource
應如下所示
var dataSource = [
{location: "France", January: 79, February: 81, March: 23},
{location: "Germany", January: 98, February: 83, March: 100},
{location: "Japan", January: 96, February: 100, March: 11} ];
我嘗試了以前的線程的許多解決方案,但我沒有得到我想要的確切結果。 以下是我的一些想法
var dataSource = [ {location: "France", January: 79, February: 81, March: 23}, {location: "Germany", January: 98, February: 83}, {location: "Japan", January: 96, March: 11} ]; var Months = ["January","February","March"]; dataSource.forEach(function(element) { Months.forEach(function(item) { if (!(item in element)) { //Object.assign(dataSource, {item: 100}); //dataSource = {...dataSource, ...{item: 100}} dataSource.push({item: 100}); } }); }); console.log(dataSource);
謝謝你的建議。
您可以使用Array.map()
來迭代dataSource
數組。 使用內部Array.reduce()
迭代Months,並向當前對象添加缺少的月份:
const dataSource = [{"location":"France","January":79,"February":81,"March":23},{"location":"Germany","January":98,"February":83},{"location":"Japan","January":96,"March":11}]; const Months = ["January","February","March"]; const result = dataSource.map(o => Months.reduce((obj, m) => m in obj ? obj : { ...obj, [m]: 100 }, o) ); console.log(result);
您可以遍歷源和月,將源上的月份設置為已有的值,或默認為100。
var dataSource = [{ location: "France", January: 79, February: 81, March: 23 }, { location: "Germany", January: 98, February: 83 }, { location: "Japan", January: 96, March: 11 } ]; var Months = ["January", "February", "March"]; dataSource.forEach(function(element){ Months.forEach(function(month){ if (element[month] === undefined) element[month] = 100; }); }); console.log(dataSource);
或者,您可以通過在其上分配數組的每個對象來創建包含結果月份及其值的哈希值,您可以直接使用它們。 因此,您可以使用相同的哈希處理N
個輸入(數組)。
結果哈希示例: {January: 100, February: 100, March: 100}
然后你可以做N
個這樣的時間:
dataSource1.map(d => ({...hash, ...d}));
dataSource2.map(d => ({...hash, ...d}));
dataSource3.map(d => ({...hash, ...d}));
這是一個例子:
let dataSource = [{"location":"France","January":79,"February":81,"March":23},{"location":"Germany","January":98,"February":83},{"location":"Japan","January":96,"March":11}], Months = ["January","February","March"], hash = Months.reduce((r, e) => ({...r, [e]: 100}), {}), res = dataSource.map(d => ({...hash, ...d})); console.log('This is your hash: ', hash); //This is the hash generated one time console.log('This is your result: ', res); //this is the result against that input
也許這可以幫到你一些。 只需在開發人員工具或其他東西中運行test()
,然后查看console.log輸出
注意uniqueKeys
對象,因為它主要邏輯的中心位置和hasOwnProperty的東西
function test() {
var uniqueKeys = {location: ''}; //default to have location set
var Months = ['January', 'February', 'March'];
for (var i = 0; i < Months.length; i++) {
uniqueKeys[Months[i]] = ''; //just populate the object from the array to use soon
}
for (var i = 0; i < dataSource.length; i++) {
var validObject = true;
Object.keys(uniqueKeys).forEach((e,j) => {
if (dataSource[i].hasOwnProperty(e) === false) {
validObject = false;
}
});
if (validObject === false) {
console.log('add to', dataSource[i], 'to balance it out');
}
}
}
希望它有助於一些〜歡呼
可能是這個短暫的解決方案,它會迭代幾個月並檢查數據源的每條記錄中是否存在該月份。 如果沒有,則在您的情況下添加默認值100
for(i=0;i<dataSource.length;i++){
for(j=0;j<Months.length;j++){
if(Months[j] in dataSource[i]){
}
else{
dataSource[i][Months[j]] = 100;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.