嵌套对象中取对应的值并重组
示例: 对以下数据进行处理,数据结构为两层嵌套
const result = [
{
"skus": [
{
"k_id": 10740,
"k": "颜色",
"v_id": 24,
"v": "颜色1"
}
],
"price": 15,
"stock": 1
},
{
"skus": [
{
"k_id": 10740,
"k": "颜色",
"v_id": 83,
"v": "颜色2"
}
],
"price": 16,
"stock": 2
}
]
const temp = result.map(item=>{
const children = item.skus.map(every=>{
return{
name:every.k,
value:every.v
}
})
return {
price:item.price,
sepc_list:children
}
})
数组对象有多层数据只保留两层
原数据:
let nav = [{
name: "用户中心",
code: "userManage",
url: "",
component: "",
children: [{
name: "角色管理",
code: "userRole",
url: "",
component: "",
children: [{
name: "新建",
code: "create"
}]
},
{
name: "用户管理",
code: "userList",
url: "",
component: "",
children: [{
name: "编辑",
code: "redact"
}]
}
]
},
{
name: "系统管理",
code: "systemManage",
url: "",
component: "",
children: [{
name: "应用管理",
code: "system",
url: "",
component: "",
children: [{
name: "新建",
code: "create"
}]
}]
}
]
得到数据:
[{
name: "用户中心",
code: "userManage",
url: "",
component: "",
children: [{
name: "角色管理",
code: "userRole",
url: "",
component: ""
},
{
name: "用户管理",
code: "userList",
url: "",
component: ""
}
]
},
{
name: "系统管理",
code: "systemManage",
url: "",
component: "",
children: [{
name: "应用管理",
code: "system",
url: "",
component: ""
}]
}
]
lodash
const _ = require('lodash')
function filter(arr) {
let result = _.cloneDeepWith(arr);
for (let firstLayer of result) {
if (firstLayer.children) {
for (let secondLayer of firstLayer.children) {
delete secondLayer.children;
}
}
}
return result;
}
let result = filter(nav);
console.log("result",result[0].children[0]);
console.log("原数据",nav[0].children[0]);
const result = nav.map(item => {
const children = item.children.map(every => {
return {
name: every.name,
code: every.code,
url: every.url,
component: every.component,
};
})
return {
...item,
children: children
};
})
const res = nav.map((v) => {
v.children = v.children.map((c) => {
Reflect.deleteProperty(c, 'children')
return c
})
return v
})
const fn = (data, limited = 1) =>
data.map(({ children, ...rest }) =>
({
...rest,
...(limited === 2 ? {} : { children: fn(children, limited + 1) })
})
);
https://github.com/paularmstrong/normalizr
递归树
https://segmentfault.com/a/1190000018692367
根据某个值从数组中匹配完整对象
场景:有一串值。根据这些值去匹配它在对象数组中的完整值
//拿到的数据
['测试', '测试2']
//完整的数据
[
{
auth_ids: null,
role_id: 15,
role_name: "测试"
},
{
auth_ids: null,
role_id: 15,
role_name: "测试2"
}
]
value.forEach(item => {
const tempIn = list.filter(listItem => {
return listItem.role_name == item
})
temp.push(...tempIn)
})
键值对象转换为数组对象
{ 12: a, 32: b, 43: c} 转换为
[{12:a}, {32:b}, {43: c}]
// 最好不用`for-in`,`for-in`会把原型链的所有可枚举的属性枚举出来。
// 可以用ES5的`Object.keys()`,只枚举当前对象的可枚举是属性。
var obj = { 12: 'a', 32: 'b', 43: 'c'};
var result = Object.keys(obj).map((el) => {
return {[el]: obj[el]};
});
console.log(result);
// 也可以使用`ES8`的`Object.entries()`
var result2 = Object.entries(obj).map(el => {
console.log(el[0], el[1]);
return {[el[0]]: el[1]};
});
console.log(result2);
双层嵌套的数组如何对其中的某个值进行相加
let arr = [
[
{
name: 'a',
number: 10
}
],
[
{
name: 'a',
number: 10
}
],
[
{
name: 'a',
number: 10
}
]
]
方案一:扁平化数组在进行相加
let flatArr = arr.flat()
let res = flatArr.reduce((pre, cur) => {
return pre + cur.number
}, 0)
扁平化数组转为树形数据
https://juejin.cn/post/7026744869158649892
专题:多维数组的处理