循环三级数组

时间:2018-05-31 21:58:44

标签: javascript arrays loops

enter image description here你好我有一个有三个级别的数组。第三级包含一个数组,每个数组最多包含3个对象的数组。每个对象内部都有自己的数据。我希望遍历包含对象的每个数组并收集状态字符串的计数。它将要么通过",#34;失败"。数据是动态的,并使用带有node.js的API路由使用JSON.stringify传递到UI。我可以使用

访问整个数组
 $.getJSON("/automation/api/pos_reports")
.then(addData)

  function addData(data) {
  var postotal = (data.posreports.length)
  $('#posTotal').append(postotal);

  let posreports = data.posreports;
  console.log(posreports)

我被告知使用Ramda.js是最容易使用的。任何帮助表示赞赏。打开使用本机JS,jQuery或像Ramda.js这样的库。感谢。

示例数组:

let data = [
    [
        [    
            {result: "Passed"},
            {result: "Failed"},
            {}
        ],
        [
            {result: "Passed"},
            {result: "Passed"},
            {result: "Failed"}
        ],
        [
            {result: "Passed"},
            {result: "Passed"},
            {result: "Failed"}
        ],
        [
            {result: "Passed"},
            {result: "Passed"},
            {result: "Failed"}
        ],
        [
            {result: "Failed"},
            {},
            {}
        ]
    ]
]

4 个答案:

答案 0 :(得分:1)

您可以使用map返回一个对象数组,包含pass和failed。利用数组映射方法:D

let data = [
[
  [
    {result: "Passed"},
    {result: "Failed"},
    {}
  ],
  [
     {result: "Passed"},
    {result: "Passed"},
    {result: "Failed"}
  ],
  [
     {result: "Passed"},
    {result: "Passed"},
    {result: "Failed"}
  ],
   [
     {result: "Passed"},
    {result: "Passed"},
    {result: "Failed"}
  ],
   [
     {result: "Failed"},
    {},
    {}
  ]
 ]
]

function CountPassFail(arr)
{
  let hash = {'Passed': 0, 'Failed':0}
  arr.forEach(val => {
    if(!val.result) return;
    let res = val.result;
    hash[res] += 1;
  })
  return hash
}

console.log(data[0].map(cell => CountPassFail(cell)))

答案 1 :(得分:1)

简单循环足以实现您想要的功能,并且不会使用任何额外的资源。 :)

@Edit:您提供的示例数组实际上与您的实际数据不同。在示例中,您有一个三维数组,在数据中,您有一个对象数组,其中包含对象数组。 :)这就是为什么代码需要被轻微改变。



ligatures




答案 2 :(得分:0)

此外,如果要合并为一个哈希

,则可以使用reduce而不是map

let data = [
[
  [
    {result: "Passed"},
    {result: "Failed"},
    {}
  ],
  [
     {result: "Passed"},
    {result: "Passed"},
    {result: "Failed"}
  ],
  [
     {result: "Passed"},
    {result: "Passed"},
    {result: "Failed"}
  ],
   [
     {result: "Passed"},
    {result: "Passed"},
    {result: "Failed"}
  ],
   [
     {result: "Failed"},
    {},
    {}
  ]
 ]
]

function CountPassFail(arr)
{
  let hash = {'Passed': 0, 'Failed':0}
  arr.forEach(val => {
    if(!val.result) return;
    let res = val.result;

    hash[res] += 1;
  })
  return hash
}

let count = data[0].reduce((hash,cell) => {
  cellHash = CountPassFail(cell);
  hash.Passed += cellHash.Passed;
  hash.Failed += cellHash.Failed;
  
  return hash;
  }, {Passed: 0, Failed : 0});
  
  console.log(count);

答案 3 :(得分:0)

您可以尝试将数组展平并缩小为一个结果:

const data = [
  [
    [
      { result: "Passed" },
      { result: "Failed" },
      {}
    ],
    [
      { result: "Passed" },
      { result: "Passed" },
      { result: "Failed" }
    ],
    [
      { result: "Passed" },
      { result: "Passed" },
      { result: "Failed" }
    ],
    [
      { result: "Passed" },
      { result: "Passed" },
      { result: "Failed" }
    ],
    [
      { result: "Failed" },
      {},
      {}
    ]
  ]
];

const flatten = array =>
  array.reduce(
    (result,item)=>
      Array.isArray(item)
        ? result.concat(flatten(item))
        : result.concat(item),
    []
  )
;

console.log(
  flatten(data)
  .reduce(
    (result,item)=>{
      if(item.result==="Passed"){
        result.passed++;
      }
      if(item.result==="Failed"){
        result.failed++;
      }
      return result;
    },
    {passed:0,failed:0}
  )
);

如果你想要一个不同的总数格式,那么这就是你的想法:

const data = [
  [
    [
      { result: "Passed" },
      { result: "Failed" },
      {}
    ],
    [
      { result: "Passed" },
      { result: "Passed" },
      { result: "Failed" }
    ],
    [
      { result: "Passed" },
      { result: "Passed" },
      { result: "Failed" }
    ],
    [
      { result: "Passed" },
      { result: "Passed" },
      { result: "Failed" }
    ],
    [
      { result: "Failed" },
      {},
      {}
    ]
  ]
];

const flatten = array =>
  array.reduce(
    (result,item)=>result.concat(item),
    []
  )
;

console.log(
  flatten(data)
  .reduce(
    (result,item)=>
      result.concat(
        item.reduce(
          (result,item)=>{
            if(item.result==="Passed"){
              result.passed++;
            }
            if(item.result==="Failed"){
              result.failed++;
            }
            return result;
          },
          {passed:0,failed:0}
        )
      ),
      []
  )
);