首页 > 解决方案 > 缺少 3+n 个值的对象子数组(基于前 2 个值)

问题描述

我有一个对象数组(Python 术语中的字典),其中 FirstValue 的可能值为or_1111__2222_SecondValue可能值为_first_or _second_or _third_。因此,在一个场景矩阵中,我们有 6 个案例。

现在,我正在尝试为每种情况创建子数组,而不会丢失其他对象的任何数据。在下面的输出中,如果我关注第一个场景 (_1111_和),当ThirdValue_first_时,我会丢失数据。VVV

有没有办法替代下面的 reduce 函数,还是我必须从完全不同的心态开始?(在这种情况下,我不会汇总数字)。

var json_data = {"headers":["FirstValue","SecondValue","ThirdValue","FourthValue","FifthValue","NumberOne","NumberTwo"],"rows":[["_2222_","_first_","CPV","unknown","_2222_",92310.8671,5226.386074028007],["_2222_","_first_","WWW","known","_2222_",92310.8671,5226.386074028007],["_2222_","_second_","VVV","unknown","_2222_",92310.8671,null],["_2222_","_third_","VVV","unknown","_2222_",92310.8671,5226.386074028007],["_1111_","_first_","VVV","unknown","random",197977.2658,36169.2693355163],["_1111_","_first_","WWW","unknown","random",197977.2658,36169.2693355163],["_1111_","_second_","VVV","unknown","random",197977.2658,36169.2693355163],["_1111_","_third_","WWW","display","random",26536.9836,1957.2823939366558]]};
 
var dataRows = json_data.rows;
var dataHeaders = json_data.headers;
var json =[];

//Convert JSON to dictionary
for (i = 0; i < dataRows.length; i++) 
{
  var object = {};
  for (j = 0; j < dataHeaders.length; j++) 
  {  
    object[dataHeaders[j]] = dataRows[i][j];
  }
  json.push(object);
}

var res = json.reduce((r, e) => {
  let {SecondValue, FirstValue} = e;
  r[FirstValue] = r[FirstValue] || {}
  r[FirstValue][SecondValue] = r[FirstValue][SecondValue] || {}
  r[FirstValue][SecondValue] = r[FirstValue][SecondValue] || {}
  Object.assign(r[FirstValue][SecondValue], e);
  return r;
}, {})

var array = Object.values(res).map(Object.values)

//Print the _1111_ branch
if("_1111_" in res){
	var _1111_ = res._1111_;
}

    //Print the _1111_ branch
    if("_first_" in _1111_){
      var _first_ = _1111_._first_;
      document.getElementById("one").innerHTML = JSON.stringify(_first_, null, 2);
    }
/*   
     if("_second_" in _1111_){
      var _second_ = _1111_._second_;
      document.getElementById("two").innerHTML = JSON.stringify(_second_, null, 2);
    }   

    if("_third_" in _1111_){
      var _third_ = _1111_._third_;
      document.getElementById("three").innerHTML = JSON.stringify(_third_, null, 2);
    }
*/
/*
//Print the _2222_ branch
if("_2222_" in res){
	var _2222_ = res._2222_;
} 

    //Print the _2222_ branch
    if("_first_" in _2222_){
      var _first_ = _2222_._first_;
      document.getElementById("four").innerHTML = JSON.stringify(_first_, null, 2);
    }

    if("_second_" in _2222_){
      var _second_ = _2222_._second_;
      document.getElementById("five").innerHTML = JSON.stringify(_second_, null, 2);
    }

    if("_third_" in _2222_){
      var _third_ = _2222_._third_;
      document.getElementById("six").innerHTML = JSON.stringify(_third_, null, 2);
    }
*/
<h3>_1111_ - _first_</h3>
<div style="background:grey;" id="one"></div>
<!--
<h3>_1111_ - _second_</h3>
<div style="background:green;" id="two"></div>
<h3>_1111_ - _third_</h3>
<div style="background:grey;" id="three"></div>
<h3>_2222_ - _first_</h3>
<div style="background:green;" id="four"></div>
<h3>_2222_ - _second_</h3>
<div style="background:grey;" id="five"></div>
<h3>_2222_ - _third_</h3>
<div style="background:green;" id="six"></div>
-->

PS:我保留了未注释的代码,因为这不是最终版本。重复的代码可能会被替换。

标签: javascript

解决方案


随着Object.assign(r[FirstValue][SecondValue], e)您使用 中的新数据覆盖该ThirdValue属性的任何先前数据e。如果您需要同时保留两者,那么最好为 FirstValue 和 SecondValue 的每个组合使用一个数组:

let {SecondValue, FirstValue} = e;
r[FirstValue] = r[FirstValue] || {};
r[FirstValue][SecondValue] = r[FirstValue][SecondValue] || [];  
//                                                         ^^
r[FirstValue][SecondValue].push(e);
//                         ^^^^^^^
return r;

然后,您需要调整其余处理以适应此修改后的数据结构。


推荐阅读