如何在Javascript / Jquery

时间:2019-03-07 18:37:14

标签: javascript jquery arrays

我有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”,因此我需要按下键和值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);

谢谢您的建议。

5 个答案:

答案 0 :(得分:6)

您可以使用Array.map()来迭代dataSource数组。使用内部Array.reduce()迭代月份,并将缺少的月份添加到当前对象:

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);

答案 1 :(得分:2)

您可以遍历源和月份,将源上的月份设置为它们已经拥有的值,或者默认为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);

答案 2 :(得分:1)

否则,您可以通过将数组中的每个对象分配在其上来创建带有结果月份及其值的哈希,您可以直接使用这些哈希及其值。因此,您可以处理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

答案 3 :(得分:0)

也许这可以帮助您一些。只需在开发人员工具或类似工具中运行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');

        }
    }
}

希望对您有所帮助 〜欢呼

答案 4 :(得分:0)

这可能是一个简短的解决方案,它会在几个月中进行迭代,并检查数据源的每个记录中是否存在该月份。如果没有,则添加默认值,以您的情况为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;
            }   
        }
    }