JS基于键值循环遍历数组并添加值

时间:2018-03-11 14:30:41

标签: javascript arrays object

我在数组中有以下对象: -

[
 {"score": 5, "question": 0, "weight": 2},
 {"score": 4, "question": 1, "weight": 2},
 {"score": 3, "question": 0, "weight": 4},
 {"score": 4, "question": 1, "weight": 4},
 {"score": 2, "question": 2, "weight": 4},
 {"score": 8, "question": 0, "weight": 2}
]

我正在尝试遍历数组,所以我有以下输出,所以我能够对结果运行一些数学: -

[
  [
    {"score": 5, "question": 0, "weight": 2},
    {"score": 4, "question": 1, "weight": 2}
  ],
  [
    {"score": 3, "question": 0, "weight": 4},
    {"score": 4, "question": 1, "weight": 4},
    {"score": 2, "question": 2, "weight": 4}
  ],
  [
    {"score": 8, "question": 0, "weight": 2}
  ]
];

我是否有动态的方式让array1看起来像array2?

我正在使用平板JS请不要jQuery答案。

提前致谢。

**注意**

有时每个部分都会有更多或更少的值,这就是我要求它变为动态的原因。

7 个答案:

答案 0 :(得分:2)

您可以使用reduce()方法执行此操作,只需跟踪最终数组的当前索引。



const data =[
  {score: 5, question: 0, weight: 2},
  {score: 4, question: 1, weight: 2},
  {score: 3, question: 0, weight: 4},
  {score: 4, question: 1, weight: 4},
  {score: 2, question: 2, weight: 4},
  {score: 8, question: 0, weight: 2}
]

const result = data.reduce(function(r, e, i) {
  if(i == 0) r = {values: [], counter: 0}
  if(e.question == 0 && i != 0) r.counter++
  if(!r.values[r.counter]) r.values[r.counter] = [e]
  else r.values[r.counter].push(e)
  return r;
}, {}).values

console.log(result)




答案 1 :(得分:1)

您可以查看weight,如果不同,请选择一个新组。



var data = [{ score: 5, question: 0, weight: 2 }, { score: 4, question: 1, weight: 2 }, { score: 3, question: 0, weight: 4 }, { score: 4, question: 1, weight: 4 }, { score: 2, question: 2, weight: 4 }, { score: 8, question: 0, weight: 2 }],
    grouped = data.reduce(function (r, o, i, a) {
        if ((a[i - 1] || {}).weight !== o.weight) {
            r.push([]);
        }
        r[r.length - 1].push(o);
        return r;
    }, []);

console.log(grouped);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 2 :(得分:0)

您可以使用Array.reduce()将第1个数组转换为第2个数组。只要当前对象权重与lastWeight不匹配,请添加另一个子阵列。始终将当前项目推送到最后一个子阵列:



const arr = [
  {score: 5, question: 0, weight: 2},
  {score: 4, question: 1, weight: 2},
  {score: 3, question: 0, weight: 4},
  {score: 4, question: 1, weight: 4},
  {score: 2, question: 2, weight: 4},
  {score: 8, question: 0, weight: 2}
];

let lastWeight = null;
const result = arr.reduce((r, o) => {
  if(lastWeight === null || o.weight !== lastWeight) {
    lastWeight = o.weight;
    r.push([]);
  }
  r[r.length - 1].push(o);
  
  return r;
}, []);

console.log(result);




答案 3 :(得分:0)

var oldA = [
 {"score": 5, "question": 0, "weight": 2},
 {"score": 4, "question": 1, "weight": 2},
 {"score": 3, "question": 0, "weight": 4},
 {"score": 4, "question": 1, "weight": 4},
 {"score": 2, "question": 2, "weight": 4},
 {"score": 8, "question": 0, "weight": 2}
];

var newA = [];
var prevW = 0;
var prevA;

for (var i = 0; i < oldA.length; i++) {
  if (oldA[i].weight != prevW) {
    prevA = [];
    newA.push(prevA);
    prevW = oldA[i].weight;
  }
  prevA.push(oldA[i]);
}

console.log(newA);

答案 4 :(得分:0)

您可以使用array#reduce对数组进行分组。检查question的值是否为0,然后推送一个新数组并将对象添加到其中。

&#13;
&#13;
var data = [ {"score": 5, "question": 0, "weight": 2}, {"score": 4, "question": 1, "weight": 2}, {"score": 3, "question": 0, "weight": 4}, {"score": 4, "question": 1, "weight": 4}, {"score": 2, "question": 2, "weight": 4}, {"score": 8, "question": 0, "weight": 2}],
    result = data.reduce((r,o) => {
      if(o.question == 0)
        r.push([]);
      r[r.length - 1].push(o);
      return r;
    },[]);
console.log(result);
&#13;
&#13;
&#13;

答案 5 :(得分:0)

&#13;
&#13;
let scores = [
 {"score": 5, "question": 0, "weight": 2},
 {"score": 4, "question": 1, "weight": 2},
 {"score": 3, "question": 0, "weight": 4},
 {"score": 4, "question": 1, "weight": 4},
 {"score": 2, "question": 2, "weight": 4},
 {"score": 8, "question": 0, "weight": 2}
]

let groupedScores = [], group = [];

scores.forEach((entry) => {
  if(entry.question === 0) { 
    if (group.length) { 
      groupedScores.push(group); 
    }
    group = []; 
  }
  group.push(entry);
})
groupedScores.push(group)

console.log(groupedScores)
&#13;
&#13;
&#13;

答案 6 :(得分:0)

如果仅按question: 0分割,则会略微简化:

&#13;
&#13;
data = [ { score: 5, question: 0, weight: 2 }, { score: 4, question: 1, weight: 2 },
         { score: 3, question: 0, weight: 4 }, { score: 4, question: 1, weight: 4 },
         { score: 2, question: 2, weight: 4 }, { score: 8, question: 0, weight: 2 } ]

result = data.reduce((r, v) => (v.question ? r[r.length-1].push(v) : r.push([v]), r), [])

console.log( result )
&#13;
&#13;
&#13;

相关问题