我有一个像这样的JSON文件:
[
{
"type": -1,
"name": "First Group"
},
{
"type": 2,
"name": "A"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Second Group"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Third Group"
},
{
"type": 4,
"name": "C"
},
{
"type": 2,
"name": "A"
}
]
我想在许多阵列上划分这个对象数组,然后用" type":-1 字段对它进行分组。
例如,对于当前的JSON,我希望得到类似的结果:
1)
[
{
"type": -1,
"name": "First Group"
},
{
"type": 2,
"name": "A"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
}
]
2)
[
{
"type": -1,
"name": "Second Group"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
}
]
3)
[
{
"type": -1,
"name": "Third Group"
},
{
"type": 4,
"name": "C"
},
{
"type": 2,
"name": "A"
}
]
我的问题在下一个:
在TypeScript / JavaScript上是否存在某些功能?或者需要创建特定的东西。如果不存在,那么如何为此创建一个更好,更快的解决方案呢?
答案 0 :(得分:0)
没有内置函数或方法来获取想要的组。
您可以为每个找到的type
等于-1
获取一个新数组,并为所有其他类型获取结果集的最后一个插入数组。
这显然仅适用于已排序的数组。
var array = [{ type: -1, name: "First Group" }, { type: 2, name: "A" }, { type: 3, name: "B" }, { type: 4, name: "C" }, { type: -1, name: "Second Group" }, { type: 3, name: "B" }, { type: 4, name: "C" }, { type: -1, name: "Third Group" }, { type: 4, name: "C" }, { type: 2, name: "A" }],
grouped = array.reduce(function (r, a) {
if (a.type === -1) {
r.push([a]);
} else {
r[r.length - 1].push(a);
}
return r;
}, []);
console.log(grouped);

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

答案 1 :(得分:0)
const arr = [
{
"type": -1,
"name": "First Group"
},
{
"type": 2,
"name": "A"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Second Group"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Third Group"
},
{
"type": 4,
"name": "C"
},
{
"type": 2,
"name": "A"
}
];
const chunks = [];
let chunk = [];
arr.forEach((item) => {
if(item.type === -1) {
if(chunk.length) {
chunks.push(chunk);
}
chunk = [];
}
chunk.push(item);
});
chunks.push(chunk);
console.log(chunks);
答案 2 :(得分:0)
尝试遵循简单的逻辑。
var data = [
{
"type": -1,
"name": "First Group"
},
{
"type": 2,
"name": "A"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Second Group"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Third Group"
},
{
"type": 4,
"name": "C"
},
{
"type": 2,
"name": "A"
}
];
var output = [],
_index = 0;
data.forEach(function(obj){
if(obj.type == -1){
output.push([obj]);
_index++;
} else {
output[_index-1].push(obj);
}
});
console.log(output);
答案 3 :(得分:0)
使用Array.prototype.reduce
:
const collection = [{
"type": -1,
"name": "First Group"
},
{
"type": 2,
"name": "A"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Second Group"
},
{
"type": 3,
"name": "B"
},
{
"type": 4,
"name": "C"
},
{
"type": -1,
"name": "Third Group"
},
{
"type": 4,
"name": "C"
},
{
"type": 2,
"name": "A"
}
]
const makeReducerByTypeValue = value => (result, el, index) => {
if (el.type === value) {
result.sorted.push([])
if (index > 0) result.currentIndex++
}
result.sorted[result.currentIndex].push(el)
return result
}
const sorted = collection.reduce(makeReducerByTypeValue(-1), {
currentIndex: 0,
sorted: []
}).sorted
sorted.forEach(el => console.log(el))