Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在对象中的嵌套数组上迭代以整理信息_Javascript - Fatal编程技术网

Javascript 在对象中的嵌套数组上迭代以整理信息

Javascript 在对象中的嵌套数组上迭代以整理信息,javascript,Javascript,我有以下对象数组: let array = [ {key: 'Newcastle', values[ {key: 'ID1000', values[ {name: 'Jeff', cust_id: "ID1000", status: 'sold'}, {name: 'Jeff', cust_id: "ID1000", status: 'pending'}, ]} {key: 'ID2000', values [ {name: 'Bob', cust_id: "

我有以下对象数组:

let array = [
{key: 'Newcastle', values[
  {key: 'ID1000', values[
    {name: 'Jeff', cust_id: "ID1000", status: 'sold'},
    {name: 'Jeff', cust_id: "ID1000", status: 'pending'},
  ]}
  {key: 'ID2000', values [
    {name: 'Bob', cust_id: "ID2000", status: 'sold'}
  ]}
]}
{key: 'London', values [
 {key: 'ID3000', values[
  {name: 'Gary', cust_id: "ID3000", status: 'sold'},
 ]}
 {key: 'ID4000', values[
  {name: 'Mary', cust_id: "ID4000", status: 'interest'},
  {name: 'Mary', cust_id: "ID4000", status: 'interest'},
  {name: 'Mary', cust_id: "ID4000", status: 'pending'},
 ]}
]}
]
我一直在尝试将其重构为如下内容:

[
 {Location: 'Newcastle, customers: 2, sold: 2, pending: 1, interest: 0},
 {Location: 'London', customers: 2, sold: 1, pending: 1, interest: 2}
]
因此,我试图统计状态事件的数量,并相应地进行整理

当我尝试对嵌套数组进行迭代,然后尝试将迭代结果冒泡到最终对象时,我会迷失方向。我得到的最接近的结果是:

function transform(array) {
      let arr = []
      array.forEach(function(x) {
        function soldCount() {
          x.values.forEach(function(x) {

            let sold = x.values.forEach(function(x) {
              let soldTrue = 0
              if (x.status === "sold") {
                soldTrue++
              }
              console.log(soldTrue)
              if (soldTrue > 0) {
                return soldTrue
              }
            })

          })
        }
        let obj = {
          location: x.key,
          customers: x.values.length,
          sold: soldCount()
        }

        arr.push(obj)
      })
      return arr
    }
这将尝试在对象中的每个数组上进行迭代,并尝试返回它找到的“已售出”状态的数量。console语句确实返回一个数字,但由于“forEach”,它为数组中的每个项返回多个条目


我陷入了大量在嵌套数组上迭代的forEach循环中。我怀疑这可能不是我想要实现的目标的正确方法。

您可以使用对象计数
状态
并构建一个新对象

var数组=[{key:'Newcastle',值:[{key:'ID1000',值:[{name:'Jeff',cust_id:'ID1000',status:'seed'},{name:'Jeff',cust_id:'ID1000',status:'pending'},]},{key:'Bob',cust_id:'ID2000',status:'seed'}]},{key:'London值:[{key:'ID3000',value:[{name:'Gary',cust"id:'“ID3000”,状态:'sell'},]},{key:'ID4000',值:[{name:'Mary',cust_id:'ID4000',状态:'interest'},{name:'Mary',cust_id:'ID4000',状态:'pending'},{name:'Mary',cust_id:'ID4000',status:'pending'},
结果=数组.map({key:Location,values})=>{
var数据={位置,客户:0,已售出:0,待处理:0,利息:0};
values.forEach(({values})=>{
数据。客户++;
forEach({status})=>data[status]++);
});
返回数据;
});
console.log(结果);
.as控制台包装{max height:100%!important;top:0;}
看看这个:

let array = [
{
  key: 'Newcastle', 
  values: [
  {key: 'ID1000', 
   values:[
    {name: 'Jeff', cust_id: "ID1000", status: 'sold'},
    {name: 'Jeff', cust_id: "ID1000", status: 'pending'},
  ]},
  {
    key: 'ID2000', 
    values :[
    {name: 'Bob', cust_id: "ID2000", status: 'sold'}
  ]
  }
]},
{key: 'London', values :[
 {key: 'ID3000', values:[
  {name: 'Gary', cust_id: "ID3000", status: 'sold'},
 ]},
 {key: 'ID4000', values:[
  {name: 'Mary', cust_id: "ID4000", status: 'interest'},
  {name: 'Mary', cust_id: "ID4000", status: 'interest'},
  {name: 'Mary', cust_id: "ID4000", status: 'pending'},
 ]}
]}
]

function countProp(arr, prop) {
    let n = 0;
    arr.forEach((items) => {
      items.values.forEach(item => {
            if (item.status === prop) {
                n += 1;
            }
        })
    });
    return n;
}

const n = array.map(item => {
  const a = {};
  a.customers = item.values.length;
  a.location = item.key;
  a.sold = countProp(item.values, "sold");
  a.pending = countProp(item.values, "pending");
  a.interest = countProp(item.values, "interest");
  return a;
});
这给

[
 {Location: 'Newcastle, customers: 2, sold: 2, pending: 1, interest: 0},
 {Location: 'London', customers: 2, sold: 1, pending: 1, interest: 2}
]

假设您正在使用
let
。 我已经用ES6编写了我的解决方案,很容易获得位置和客户数量

具有挑战性的部分是累积每种状态的状态计数,您可以使用reduce with object作为初始值,使用状态类型作为初始值为0的键,如我的解决方案所示

let数组=[
{键:'Newcastle',值:[
{键:“ID1000”,值:[
{姓名:'Jeff',客户id:'ID1000',状态:'已售出'},
{姓名:'Jeff',客户id:'ID1000',状态:'pending'}
]},
{键:“ID2000”,值:[
{姓名:'Bob',客户id:'ID2000',状态:'salled'}
]}
]},
{键:“伦敦”,值:[
{键:“ID3000”,值:[
{姓名:'Gary',客户id:'ID3000',状态:'salled'}
]},
{键:“ID4000”,值:[
{姓名:'Mary',客户id:'ID4000',状态:'interest'},
{姓名:'Mary',客户id:'ID4000',状态:'interest'},
{姓名:'Mary',客户id:'ID4000',状态:'pending'}
]}
]}
];
const newArray=array.map((a)=>{
const Location=a.key;
const customer=a.values.length;
常量状态=a.values.reduce((acc,value)=>{
value.values.forEach((v1)=>{
acc[v1.状态]=acc[v1.状态]+1;
});
返回acc;
},{已售出:0,待决:0,利息:0});
返回{位置,客户,…状态};
});

console.log(newArray);
我将此标记为已接受,因为我喜欢它的包含方式和简洁性。我应该想到,如果我想创建一个新数组,我应该查看.map而不是淹没在.forEach循环中