Javascript填充缺少对象和值的数组

时间:2017-02-10 19:00:39

标签: javascript arrays object

我有一个像下面这样的数组,每个索引包含不同的对象集,我想创建一个统一数据,其中每个索引中缺少的对象将使用Value:0,

var d = [
          [
            {axis:"Email",value:59,id:1},
            {axis:"Social Networks",value:56,id:2},

          ],
          [

            {axis:"Sending Money",value:18,id:6},
            {axis:"Other",value:15,id:7},

          ]
        ];

如何使用上面的数组

获得类似下面的数组
var d = [
          [
            {axis:"Email",value:59,id:1},
            {axis:"Social Networks",value:56,id:2},
            {axis:"Sending Money",value:0,id:6},
            {axis:"Other",value:0,id:7},

          ],
          [
            {axis:"Email",value:0,id:1},
            {axis:"Social Networks",value:0,id:2},
            {axis:"Sending Money",value:18,id:6},
            {axis:"Other",value:15,id:7},

          ]
        ];

3 个答案:

答案 0 :(得分:1)

有两个功能:

getAllEntries 找到所有对象并将它们存储到变量 accEntries 中。然后 accEntries 用于搜索 d 子数组中的所有匹配项。整个过程在 checkArray 中完成。

checkArray 用于获取 d 中所有找到和未找到的条目。然后使用两个数组(找到和未找到)构建一个新的子数组,其中包含具有特定值的已找到条目和/或值为0的未找到条目。

希望这会有所帮助:



var d = [
  [
  {
    axis: 'Email',
    value: 59,
    id: 1
  },
  {
    axis: 'Social Networks',
    value: 56,
    id: 2
  },
  ],
  [
    {
      axis: 'Sending Money',
      value: 18,
      id: 6
    },
    {
      axis: 'Other',
      value: 15,
      id: 7
    },
  ]
];
  

function getAllEntries(array) {
  var uniqueEntries = [];
  array.forEach(function (subarray) {
    subarray.forEach(function (obj) {
      if (uniqueEntries.indexOf(obj) === - 1) uniqueEntries.push(obj);
    });
  });
  return uniqueEntries;
}


function checkArray(array, acceptedEntries) {
  var result = [];
  array.forEach(function (subArray) {
    var subResult = [];
    var foundEntries = [];
    subArray.forEach(function (obj) {
      if (foundEntries.indexOf(obj.axis) === - 1) foundEntries.push(obj.axis);
    });
    var notFound = acceptedEntries.filter(function (accepted) {
      return foundEntries.indexOf(accepted.axis) === - 1;
    });
    foundEntries.forEach(function (found) {
      subArray.forEach(function (obj) {
        if (obj.axis === found) subResult.push(obj);
      });
    });
    notFound.forEach(function (notfound, index) {
      subResult.push({
        axis: notfound.axis,
        value: 0,
        id: notfound.id
      });
    });
    result.push(subResult);
  });
  return result;
}


var accEntries = getAllEntries(d);
var result = checkArray(d, accEntries);
console.log(result);




答案 1 :(得分:0)

您可以遍历数组以查找所有唯一对象,然后再次循环以推送与唯一键对象数组相比不存在的值。

您可以使用ES6语法查找具有属性的对象是否存在uniKeys.findIndex(obj => obj.axis === val.axis);,而使用零值推送使用d[index].push({...val, value: 0});

之类的传播语法

以下是实施的摘要

var d = [
          [
            {axis:"Email",value:59,id:1},
            {axis:"Social Networks",value:56,id:2},

          ],
          [

            {axis:"Sending Money",value:18,id:6},
            {axis:"Other",value:15,id:7},
            {axis:"Social Networks",value:89,id:2},
          ]
        ];
            
var uniKeys = [];
  $.each(d, function(index, item) {
  
              $.each(item, function(idx, val){
                    const pos = uniKeys.findIndex(obj => obj.axis === val.axis);
               
               if(pos == - 1) {
                    uniKeys.push(val);
                }
                
               })
            })
  
  $.each(d, function(index, item) {
             var temp = [];
              $.each(uniKeys, function(idx, val){
                    const pos = item.findIndex(obj => obj.axis === val.axis);
               
               if(pos == - 1) {
                    
                    d[index].push({...val, value: 0});
                    
                }
                
               })
              
            })
  console.log(d);
  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

答案 2 :(得分:-1)

shallowCopy函数(IE中不可用Object.assign)以及少于10行的新代码行怎么样?

var d = [
      [
        {axis:"Email",value:59,id:1},
        {axis:"Social Networks",value:56,id:2}
      ],
      [
        {axis:"Sending Money",value:18,id:6},
        {axis:"Other",value:15,id:7}
      ]
    ];

var newD_0 = [shallowCopy(d[0][0]), shallowCopy(d[0][1]), shallowCopy(d[1][0]), shallowCopy(d[1][1])];
var newD_1 = [shallowCopy(d[0][0]), shallowCopy(d[0][1]), shallowCopy(d[1][0]), shallowCopy(d[1][1])];

newD_0[2].id = 0; 
newD_0[3].id = 0;
newD_1[0].id = 0;
newD_1[1].id = 0;

d = [newD_0, newD_1];

function shallowCopy(obj) {
    var copy = {};
    for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
            copy[key] = obj[key];   
        }
    }
    return copy;
}

console.log(JSON.stringify(d));

结果:

[  
   [  
      {  
         "axis":"Email",
         "value":59,
         "id":1
      },
      {  
         "axis":"Social Networks",
         "value":56,
         "id":2
      },
      {  
         "axis":"Sending Money",
         "value":18,
         "id":0
      },
      {  
         "axis":"Other",
         "value":15,
         "id":0
      }
   ],
   [  
      {  
         "axis":"Email",
         "value":59,
         "id":0
      },
      {  
         "axis":"Social Networks",
         "value":56,
         "id":0
      },
      {  
         "axis":"Sending Money",
         "value":18,
         "id":6
      },
      {  
         "axis":"Other",
         "value":15,
         "id":7
      }
   ]
]