如何使用 lodash/underscorejs 拆分具有特定条件的对象数组 javascript
How to split javascript array of object with specific condition using lodash/underscorejs
我有这样的对象数组:
var data = [
{
type : "parent",
name : "A"
},
{
type : "child",
name : "1"
},
{
type : "child",
name : "2"
},
{
type : "parent",
name : "B"
},
{
type : "child",
name : "3"
}
]
并且我想将子对象移动到父对象中,由父对象拆分(没有来自子对象的给定键属于哪个父对象)。所以它只被父对象分开。为简单起见,我想将数组更改为:
[
{
type : "parent",
name : "A",
child: [
{
type : "child",
name : "1"
},
{
type : "child",
name : "2"
}
]
},
{
type : "parent",
name : "B",
child: [
{
type : "child",
name : "3"
}
]
}
]
我读过有关 chunk 的 lodash,但没用。
普通javascript版本:
var newArr = [];
var j=0;
var k=0;
for (var i = 0; i <data.length; i++) {
if(data[i].type == 'parent'){
newArr[j] = data[i];
newArr[j].children = [];
j++;
k=0;
}
else {
data[i].name = newArr[j-1].name.toLowerCase() + String.fromCharCode(k + 97)
newArr[j-1].children[k] =data[i];
k++;
}
}
console.log(newArr)
我在这里假设 parent 总是 放在 children 之前,如您的示例数据中所提供的。
还有,如果能用26+children来防止parents就好了。这会导致 String.fromCharCode(k + 97)
打印出奇怪的字符。为此,请参阅 http://www.asciitable.com/
这里有一个lodash的解决方案,可能更容易理解一些。 CodePen
一些注意事项:
- 这会修改传入数据 object - 如果这是一个问题,我们可以进行一些
_.clone()
调用。
- 这仅在每个 parent 有 26 个或更少 children 时有效,因为您选择了
name: "ab"
模式
var lastParent;
var result = _.chain(data)
.groupBy(function (item) {
if (item.type === 'parent') lastParent = item.name
return lastParent
})
.map(function (group) {
var parent = _.first(group)
parent.child = _.chain(group)
.slice(1)
.map(function (child, index) {
child.name = parent.name.toLowerCase() + String.fromCharCode(index + 97)
return child
})
.value()
return parent
})
.value()
console.log(result)
您可以使用本机 Array.prototype.reduce
function or lodash's reduce
:
var data = [{
type: "parent",
name: "A"
},
{
type: "child",
name: "1"
},
{
type: "child",
name: "2"
},
{
type: "parent",
name: "B"
},
{
type: "child",
name: "3"
}
];
// If using _.reduce then use:
// var newData = _.reduce(data, function(arr, el) {...}, []);
var newData = data.reduce(function(arr, el) {
if (el.type === 'parent') {
// If el is pushed directly it would be a reference
// from the original data object
arr.push({
type: el.type,
name: el.name,
child: []
});
} else {
arr[arr.length - 1].child.push({
type: el.type,
name: el.name
});
}
return arr;
}, []);
console.log(newData);
更新:使用更新的 ES 语言功能进行的小改动
const data = [{
type: "parent",
name: "A"
},
{
type: "child",
name: "1"
},
{
type: "child",
name: "2"
},
{
type: "parent",
name: "B"
},
{
type: "child",
name: "3"
}
];
const newData = data.reduce((arr, el) => {
if (el.type === 'parent') {
// If el is pushed directly it would be a reference
// from the original data object
arr.push({...el, child: []});
} else {
arr[arr.length - 1].child.push({...el});
}
return arr;
}, []);
console.log(newData);
for (ele in data)
{
if (!data[ele].hasOwnProperty('child') && data[ele].type=='parent')
{
data[ele].child = [];
while(data[parseInt(ele) + 1] && data[parseInt(ele) + 1].type == 'child')
{
data[ele].child.push({type: data[parseInt(ele) + 1].type, name:data[parseInt(ele) + 1].name});
data.splice(parseInt(ele) + 1, 1);
}
}
}
console.log(data);
尝试简单循环:
var current, parent, result = [], i = 0;
while(current = data[i++]){
if(current.type === "parent"){
current.child = [];
result.push(current);
parent = current
}else{
current.name = (parent.name + String.fromCharCode(parent.child.length + 97)).toLowerCase();
parent.child.push(current)
}
}
我有这样的对象数组:
var data = [
{
type : "parent",
name : "A"
},
{
type : "child",
name : "1"
},
{
type : "child",
name : "2"
},
{
type : "parent",
name : "B"
},
{
type : "child",
name : "3"
}
]
并且我想将子对象移动到父对象中,由父对象拆分(没有来自子对象的给定键属于哪个父对象)。所以它只被父对象分开。为简单起见,我想将数组更改为:
[
{
type : "parent",
name : "A",
child: [
{
type : "child",
name : "1"
},
{
type : "child",
name : "2"
}
]
},
{
type : "parent",
name : "B",
child: [
{
type : "child",
name : "3"
}
]
}
]
我读过有关 chunk 的 lodash,但没用。
普通javascript版本:
var newArr = [];
var j=0;
var k=0;
for (var i = 0; i <data.length; i++) {
if(data[i].type == 'parent'){
newArr[j] = data[i];
newArr[j].children = [];
j++;
k=0;
}
else {
data[i].name = newArr[j-1].name.toLowerCase() + String.fromCharCode(k + 97)
newArr[j-1].children[k] =data[i];
k++;
}
}
console.log(newArr)
我在这里假设 parent 总是 放在 children 之前,如您的示例数据中所提供的。
还有,如果能用26+children来防止parents就好了。这会导致 String.fromCharCode(k + 97)
打印出奇怪的字符。为此,请参阅 http://www.asciitable.com/
这里有一个lodash的解决方案,可能更容易理解一些。 CodePen
一些注意事项:
- 这会修改传入数据 object - 如果这是一个问题,我们可以进行一些
_.clone()
调用。 - 这仅在每个 parent 有 26 个或更少 children 时有效,因为您选择了
name: "ab"
模式
var lastParent;
var result = _.chain(data)
.groupBy(function (item) {
if (item.type === 'parent') lastParent = item.name
return lastParent
})
.map(function (group) {
var parent = _.first(group)
parent.child = _.chain(group)
.slice(1)
.map(function (child, index) {
child.name = parent.name.toLowerCase() + String.fromCharCode(index + 97)
return child
})
.value()
return parent
})
.value()
console.log(result)
您可以使用本机 Array.prototype.reduce
function or lodash's reduce
:
var data = [{
type: "parent",
name: "A"
},
{
type: "child",
name: "1"
},
{
type: "child",
name: "2"
},
{
type: "parent",
name: "B"
},
{
type: "child",
name: "3"
}
];
// If using _.reduce then use:
// var newData = _.reduce(data, function(arr, el) {...}, []);
var newData = data.reduce(function(arr, el) {
if (el.type === 'parent') {
// If el is pushed directly it would be a reference
// from the original data object
arr.push({
type: el.type,
name: el.name,
child: []
});
} else {
arr[arr.length - 1].child.push({
type: el.type,
name: el.name
});
}
return arr;
}, []);
console.log(newData);
更新:使用更新的 ES 语言功能进行的小改动
const data = [{
type: "parent",
name: "A"
},
{
type: "child",
name: "1"
},
{
type: "child",
name: "2"
},
{
type: "parent",
name: "B"
},
{
type: "child",
name: "3"
}
];
const newData = data.reduce((arr, el) => {
if (el.type === 'parent') {
// If el is pushed directly it would be a reference
// from the original data object
arr.push({...el, child: []});
} else {
arr[arr.length - 1].child.push({...el});
}
return arr;
}, []);
console.log(newData);
for (ele in data)
{
if (!data[ele].hasOwnProperty('child') && data[ele].type=='parent')
{
data[ele].child = [];
while(data[parseInt(ele) + 1] && data[parseInt(ele) + 1].type == 'child')
{
data[ele].child.push({type: data[parseInt(ele) + 1].type, name:data[parseInt(ele) + 1].name});
data.splice(parseInt(ele) + 1, 1);
}
}
}
console.log(data);
尝试简单循环:
var current, parent, result = [], i = 0;
while(current = data[i++]){
if(current.type === "parent"){
current.child = [];
result.push(current);
parent = current
}else{
current.name = (parent.name + String.fromCharCode(parent.child.length + 97)).toLowerCase();
parent.child.push(current)
}
}