for循环内的reduce方法 - JS
reduce method inside a for loop - JS
我正在尝试使用 for 循环遍历条目数组并使用 reduce 方法,但是在第一个条目之后减少 returns,永远不会到达第二个、第三个等条目来推送结果到数组 v.
有什么想法吗?
export const groupEventData = eventData => {
console.log(eventData)
// returns[{
// "id": "141",
// "name": "00333",
// "no_transmission": true,
// "low_battery" :true,
// },
// {
// "id": "307",
// "name": "01163",
// "no_transmission": true,
// "low_data_rate": true,
// }
// ]
// now let's group similar event types
let v = [];
let types = ["no_transmission", "low_data_rate", "low_battery"];
for (let x = 0; x <= types.length; x++) {
let data = eventData.reduce((acc, i) => {
if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
acc[i[types[x]]] = [i]
}
else if (Array.isArray(acc[i[types[x]]])) {
acc[i[types[x]]].push(i);
}
else if (typeof acc[i[types[x]]] === 'object') {
acc[i[types[x]]] = [acc[i[types[x]]]]
acc[i[types[x]]].push(i)
}
return acc;
}, {});
// Doesn't add a property for the type if there are no data
Object.keys(data).length && v.push({ [types[x]: data });
return v;
};
}
您的 for 循环中有 return v
。尝试将它撞到紧靠其下方的花括号下方。现在 for 循环只迭代一次然后返回一个值。
下面为我工作:
const eventData = [
{"id": "141","name": "00333","no_transmission": true,"low_battery": true},
{"id": "307","name": "01163","no_transmission": true,"low_data_rate": true}
]
const type = 'bc'
const types = ["no_transmission", "low_data_rate", "low_battery"]
const groupEventData = (eventData, type) => {
let v = [];
for (let x = 0; x <= types.length; x++) {
let data = eventData.reduce((acc, i) => {
if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
acc[i[types[x]]] = [i]
}
else if (Array.isArray(acc[i[types[x]]])) {
acc[i[types[x]]].push(i);
}
else if (typeof acc[i[types[x]]] === 'object') {
acc[i[types[x]]] = [acc[i[types[x]]]]
acc[i[types[x]]].push(i)
}
return acc;
}, {});
Object.keys(data).length && v.push({ [`${types[x]}_${type}`]: data });
};
return v;
}
console.log('groupEventData', groupEventData(eventData, type))
我正在尝试使用 for 循环遍历条目数组并使用 reduce 方法,但是在第一个条目之后减少 returns,永远不会到达第二个、第三个等条目来推送结果到数组 v.
有什么想法吗?
export const groupEventData = eventData => {
console.log(eventData)
// returns[{
// "id": "141",
// "name": "00333",
// "no_transmission": true,
// "low_battery" :true,
// },
// {
// "id": "307",
// "name": "01163",
// "no_transmission": true,
// "low_data_rate": true,
// }
// ]
// now let's group similar event types
let v = [];
let types = ["no_transmission", "low_data_rate", "low_battery"];
for (let x = 0; x <= types.length; x++) {
let data = eventData.reduce((acc, i) => {
if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
acc[i[types[x]]] = [i]
}
else if (Array.isArray(acc[i[types[x]]])) {
acc[i[types[x]]].push(i);
}
else if (typeof acc[i[types[x]]] === 'object') {
acc[i[types[x]]] = [acc[i[types[x]]]]
acc[i[types[x]]].push(i)
}
return acc;
}, {});
// Doesn't add a property for the type if there are no data
Object.keys(data).length && v.push({ [types[x]: data });
return v;
};
}
您的 for 循环中有 return v
。尝试将它撞到紧靠其下方的花括号下方。现在 for 循环只迭代一次然后返回一个值。
下面为我工作:
const eventData = [
{"id": "141","name": "00333","no_transmission": true,"low_battery": true},
{"id": "307","name": "01163","no_transmission": true,"low_data_rate": true}
]
const type = 'bc'
const types = ["no_transmission", "low_data_rate", "low_battery"]
const groupEventData = (eventData, type) => {
let v = [];
for (let x = 0; x <= types.length; x++) {
let data = eventData.reduce((acc, i) => {
if (!acc[i[types[x]]] && typeof i[types[x]] !== 'undefined') {
acc[i[types[x]]] = [i]
}
else if (Array.isArray(acc[i[types[x]]])) {
acc[i[types[x]]].push(i);
}
else if (typeof acc[i[types[x]]] === 'object') {
acc[i[types[x]]] = [acc[i[types[x]]]]
acc[i[types[x]]].push(i)
}
return acc;
}, {});
Object.keys(data).length && v.push({ [`${types[x]}_${type}`]: data });
};
return v;
}
console.log('groupEventData', groupEventData(eventData, type))