我有数组,我想安排在dot之前对同一个字符串的对象数组结构,我想创建一个对象数组结构,使其可编辑,所以我可以填充我的数组上的每个数据的值
pred_val
我如何安排成为对象数组结构
var arr = ["data",
"data.cell",
"data.cell.celltiming",
"data.cell.celltiming.0",
"data.cell.celltiming.1",
"data.cell.earfcn",
"data.cell.pci",
"data.cell.rsrp",
"data.cell.rsrp.0",
"data.cell.rsrp.1",
"data.cell.rsrp.2",
"data.cell.rsrq",
"data.cell.rsrq.0",
"data.cell.rsrq.1",
"data.cell.rsrq.2",
"data.cell.sinr",
"data.cell.sinr.0",
"data.cell.sinr.1",
"data.cells",
"data.cells.0",
"data.cells.0.ch",
"data.cells.0.ecno",
"data.cells.0.r99",
"data.cells.0.rscp",
"data.cells.0.sc",
"data.cells.1",
"data.cells.1.ch",
"data.cells.1.ecno",
"data.cells.1.r99",
"data.cells.1.rscp",
"data.cells.1.sc",
"data.cells.2",
"data.cells.2.ch",
"data.cells.2.ecno",
"data.cells.2.r99",
"data.cells.2.rscp",
"data.cells.2.sc",
"data.cells.3",
"data.cells.3.ch",
"data.cells.3.ecno",
"data.cells.3.r99",
"data.cells.3.rscp",
"data.cells.3.sc",
"data.id",
"data.mac",
"data.plmn",
"data.rssi",
"data.time",
"deviceID",
"time"]
我想创建一个对象数组,以便我可以填充每个键的值
答案 0 :(得分:0)
我使用了以下内容:
Array.prototype.reduce()
,累积在空对象{}
上,ref
对象的引用(accumulator
)String.prototype.split()
从数组中的当前字符串中提取单个key
s(子字符串),迭代key
以沿着key
“链”前进,
一个。如果key
对象中尚不存在accumulator
,请在该密钥上创建一个对象,即if (!ref[key]) ref[key] = {}
湾通过重新设置引用(accumulator
)继续遍历ref
对象,即ref = ref[key]
移至原始数组中的下一个字符串
var arr = ["data",
"data.cell",
"data.cell.celltiming",
"data.cell.celltiming.0",
"data.cell.celltiming.1",
"data.cell.earfcn",
"data.cell.pci",
"data.cell.rsrp",
"data.cell.rsrp.0",
"data.cell.rsrp.1",
"data.cell.rsrp.2",
"data.cell.rsrq",
"data.cell.rsrq.0",
"data.cell.rsrq.1",
"data.cell.rsrq.2",
"data.cell.sinr",
"data.cell.sinr.0",
"data.cell.sinr.1",
"data.cells",
"data.cells.0",
"data.cells.0.ch",
"data.cells.0.ecno",
"data.cells.0.r99",
"data.cells.0.rscp",
"data.cells.0.sc",
"data.cells.1",
"data.cells.1.ch",
"data.cells.1.ecno",
"data.cells.1.r99",
"data.cells.1.rscp",
"data.cells.1.sc",
"data.cells.2",
"data.cells.2.ch",
"data.cells.2.ecno",
"data.cells.2.r99",
"data.cells.2.rscp",
"data.cells.2.sc",
"data.cells.3",
"data.cells.3.ch",
"data.cells.3.ecno",
"data.cells.3.r99",
"data.cells.3.rscp",
"data.cells.3.sc",
"data.id",
"data.mac",
"data.plmn",
"data.rssi",
"data.time",
"deviceID",
"time"
]
var obj = arr.reduce((accumulator, currentValue, currentIndex) => {
let ref = accumulator;
currentValue.split(".").forEach(key => {
if (!ref[key]) ref[key] = {}
ref = ref[key];
})
return accumulator;
}, {})
console.log(JSON.stringify(obj, null, "\t"));
这让你大部分时间都可以。现在你需要做的就是:
遍历对象(第一遍),用空字符串替换空对象,然后
arr.forEach(chain => { // first pass, change empty objects to empty strings
let ref = obj;
chain.split(".").forEach(key => {
if (Object.keys(ref[key]).length === 0) {
ref[key] = ''
} else {
ref = ref[key]
}
})
})
遍历对象(第二遍),检查对象的所有键是否为数字且所有值都是空字符串,并用键数组替换此对象。
function keysAreNumbersAndValuesAreEmptyStrings(object) {
let entries = Object.entries(object);
for (let i = 0; i < entries.length; i++) {
let key = entries[i][0], value = entries[i][1];
if(isNaN(Number(key)) || typeof value !== "string" || value.length !== 0) return false;
}
return true;
}
arr.forEach(chain => { // second pass, change objects whose keys are numbers to an array of those keys
let ref = obj;
chain.split(".").forEach(key => {
if (typeof ref[key] === "object" && keysAreNumbersAndValuesAreEmptyStrings(ref[key])) {
ref[key] = Object.keys(ref[key]).map(val => Number(val));
} else {
ref = ref[key]
}
})
})
完整解决方案:https://jsfiddle.net/s2attatz/1/
var arr = ["data",
"data.cell",
"data.cell.celltiming",
"data.cell.celltiming.0",
"data.cell.celltiming.1",
"data.cell.earfcn",
"data.cell.pci",
"data.cell.rsrp",
"data.cell.rsrp.0",
"data.cell.rsrp.1",
"data.cell.rsrp.2",
"data.cell.rsrq",
"data.cell.rsrq.0",
"data.cell.rsrq.1",
"data.cell.rsrq.2",
"data.cell.sinr",
"data.cell.sinr.0",
"data.cell.sinr.1",
"data.cells",
"data.cells.0",
"data.cells.0.ch",
"data.cells.0.ecno",
"data.cells.0.r99",
"data.cells.0.rscp",
"data.cells.0.sc",
"data.cells.1",
"data.cells.1.ch",
"data.cells.1.ecno",
"data.cells.1.r99",
"data.cells.1.rscp",
"data.cells.1.sc",
"data.cells.2",
"data.cells.2.ch",
"data.cells.2.ecno",
"data.cells.2.r99",
"data.cells.2.rscp",
"data.cells.2.sc",
"data.cells.3",
"data.cells.3.ch",
"data.cells.3.ecno",
"data.cells.3.r99",
"data.cells.3.rscp",
"data.cells.3.sc",
"data.id",
"data.mac",
"data.plmn",
"data.rssi",
"data.time",
"deviceID",
"time"
]
var obj = arr.reduce((accumulator, currentValue, currentIndex) => {
let ref = accumulator;
currentValue.split(".").forEach(key => {
if (!ref[key]) ref[key] = {}
ref = ref[key];
})
return accumulator;
}, {})
arr.forEach(chain => { // first pass, change empty objects to empty strings
let ref = obj;
chain.split(".").forEach(key => {
if (Object.keys(ref[key]).length === 0) {
ref[key] = ''
} else {
ref = ref[key]
}
})
})
function keysAreNumbersAndValuesAreEmptyStrings(object) {
let entries = Object.entries(object);
for (let i = 0; i < entries.length; i++) {
let key = entries[i][0], value = entries[i][1];
if(isNaN(Number(key)) || typeof value !== "string" || value.length !== 0) return false;
}
return true;
}
arr.forEach(chain => { // second pass, change objects whose keys are numbers to an array of those keys
let ref = obj;
chain.split(".").forEach(key => {
if (typeof ref[key] === "object" && keysAreNumbersAndValuesAreEmptyStrings(ref[key])) {
ref[key] = Object.keys(ref[key]).map(val => Number(val));
} else {
ref = ref[key]
}
})
})
console.log(JSON.stringify(obj, null, "\t"));
/*
{
"data": {
"cell": {
"celltiming": [
0,
1
],
"earfcn": "",
"pci": "",
"rsrp": [
0,
1,
2
],
"rsrq": [
0,
1,
2
],
"sinr": [
0,
1
]
},
"cells": {
"0": {
"ch": "",
"ecno": "",
"r99": "",
"rscp": "",
"sc": ""
},
"1": {
"ch": "",
"ecno": "",
"r99": "",
"rscp": "",
"sc": ""
},
"2": {
"ch": "",
"ecno": "",
"r99": "",
"rscp": "",
"sc": ""
},
"3": {
"ch": "",
"ecno": "",
"r99": "",
"rscp": "",
"sc": ""
}
},
"id": "",
"mac": "",
"plmn": "",
"rssi": "",
"time": ""
},
"deviceID": "",
"time": ""
}
*/