我有一个类似下面的对象
{
UserId: "",
BidderId: "",
"1stDomestic.BidderId": "",
"1stDomestic.UserId": "234",
"1stEmployee.CreatedDate": "",
"1stIndependent.UpdatedDate": "",
"1stIndependent.CreateDate": ""
}
要求是这样,我需要对点对象键进行分组并按如下所示创建输出
{
UserId: "",
BidderId: "",
1stDomestic: [
{
BidderId="",
UserId="234"
}
],
1stEmployee: [
{
CreatedDate=""
}
],
1stIndependent: [
{
UpdatedDate="",
CreatedDate=""
}
],
lstDomestic.BidderId = "",
1stDomestic.UserId="234",
1stEmployee.CreatedDate="",
1stIndependent.UpdatedDate=""
1stIndependent.CreateDate=""
}
我尝试使用几种方法来实现这一目标。 这里,requestedData是对象
方法1
for (let prop in requestedData) {
if (prop.indexOf(".") > -1) {
mainKey[prop.split(".").pop()] = requestedData[prop];
requestedData[prop.substr(0, prop.indexOf("."))] = [mainKey];
}
}
console.log(requestedData)
以上方法为我提供了结构,但是数组数据对所有人都反映了相同的情况。
1stDomestic: [
{
BidderId="",
UserId="234",
CreatedDate="",
UpdatedDate=""
}
],
1stEmployee: [
{
BidderId="",
UserId="234",
CreatedDate="",
UpdatedDate=""
}
],
1stIndependent: [
{
BidderId="",
UserId="234",
CreatedDate="",
UpdatedDate=""
}
]
方法2
for (let prop in requestedData) {
if (prop.indexOf(".") > -1) {
arr.push({
newProp: prop.substr(0, prop.indexOf(".")), //-->1
mainKey: prop.split(".").pop(), // --> 2
value: requestedData[prop] // -->3
});
}
}
console.log(Object.assign(requestedData, groupData(arr));
groupData(arrVal) {
let key = "newProp";
return resData.reduce((previous, current) => {
previous[current[key]] && previous[current[key]].length != 0
? previous[current[key]].push(current)
: (previous[current[key]] = new Array(current));
return previous;
}, {});
}
上面的方法基于键对数据进行分组,但是随后它创建并创建了对象的单个数组,这些对象具有1,2和3中的属性
我希望这是如上所述的方式。 我现在正在解决问题,并试图找出答案。
我是这个论坛的新手,请教问题,如果我以某种方式使这个问题过于冗长和直觉,请耐心等待。 帮助将不胜感激
答案 0 :(得分:1)
您可以首先使用reduce
基于键创建嵌套对象的对象,然后将原始对象与嵌套对象合并以获得最终结果:
const data = {
UserId: "",
BidderId: "",
"1stDomestic.BidderId": "",
"1stDomestic.UserId": "234",
"1stEmployee.CreatedDate": "",
"1stIndependent.UpdatedDate": "",
"1stIndependent.CreateDate": ""
};
const nested = Object.entries(data)
.filter(([k, v]) => k.includes('.'))
.reduce((acc, [k, v]) => {
const [parent, child] = k.split('.');
acc[parent] = acc[parent] || [{}];
acc[parent][0][child] = v;
return acc;
}, {});
const result = { ...data, ...nested};
console.log(result);
答案 1 :(得分:0)
您可以使用以下解决方案。
var requestedData = {
UserId: "",
BidderId: "",
"lstDomestic.BidderId" : "",
"lstDomestic.UserId":"234",
"lstEmployee.CreatedDate":"",
"lstIndependent.UpdatedDate":"",
"lstIndependent.CreateDate":""
}
var newData = [];
var previousKey = "";
for (let prop in requestedData) {
if (prop.indexOf(".") > -1) {
if(previousKey != prop.substr(0, prop.indexOf(".")))
{
ainKey = [];
}
previousKey = prop.substr(0, prop.indexOf("."))
mainKey[prop.split(".").pop()] = requestedData[prop];
newData[prop.substr(0, prop.indexOf("."))] = [mainKey];
}
}
console.log(newData)
您可以尝试现场演示。
答案 2 :(得分:0)
如果您调用原始对象obj
,则该方法应该起作用:
Object.keys(obj).forEach(key => {
if (key.includes('.')) {
const [base, suffix] = key.split('.');
obj[base] = obj[base] || [{}];
obj[base][0][suffix] = obj[key];
}
});
console.log(obj);
或者,如果您不想修改原始对象,而是制作修改后的副本,则:
const obj2 = {};
Object.keys(obj).forEach(key => {
obj2[key] = obj[key];
if (key.includes('.')) {
const [base, suffix] = key.split('.');
obj2[base] = obj2[base] || [{}];
obj2[base][0][suffix] = obj[key];
}
});
答案 3 :(得分:0)
let orgObj={
UserId: "",
BidderId: "",
"lstDomestic.BidderId": "",//difference 1st and Ist
"1stDomestic.UserId": "234",//difference 1st and Ist
"1stEmployee.CreatedDate": "",
"1stIndependent.UpdatedDate": "",
"1stIndependent.CreateDate": ""
};
let complexKeys = Object.keys(orgObj).filter(key=>{return key.match(/.+\..+/)})
如此复杂的键现在["lstDomestic.BidderId", "1stDomestic.UserId", "1stEmployee.CreatedDate", "1stIndependent.UpdatedDate", "1stIndependent.CreateDate"]
complexKeys.forEach(eachCompleKey=>{debugger;
let firstPart= eachCompleKey.match(/^(\w+)\./)[1];
let lastPart= eachCompleKey.match(/\.(\w+)$/)[1];
if(orgObj[firstPart]==undefined){debugger;
orgObj[firstPart]=[{}];
}
orgObj[firstPart][0][lastPart]=orgObj[eachCompleKey]
})
console.log(orgObj)
输出
{
"UserId": "",
"BidderId": "",
"lstDomestic.BidderId": "",
"1stDomestic.UserId": "234",
"1stEmployee.CreatedDate": "",
"1stIndependent.UpdatedDate": "",
"1stIndependent.CreateDate": "",
"lstDomestic": [
{
"BidderId": ""
}
],
"1stDomestic": [
{
"UserId": "234"
}
],
"1stEmployee": [
{
"CreatedDate": ""
}
],
"1stIndependent": [
{
"UpdatedDate": "",
"CreateDate": ""
}
]
}
答案 4 :(得分:0)
您可以尝试以下类似方法。
let obj = {
UserId: "",
BidderId: "",
"lstDomestic.BidderId": "",
"1stDomestic.UserId": "234",
"1stEmployee.CreatedDate": "",
"1stIndependent.UpdatedDate": "",
"1stIndependent.CreateDate": ""
};
const res = Object.keys(obj).reduce((acc, mainKey) => {
let [key1, key2] = mainKey.split(".");
if (key2 && acc[key1]) {
acc[key1][0] = { ...acc[key1][0],
...{
[key2]: obj[mainKey]
}
}
} else if (key2) {
acc[key1] = [{
[key2]: obj[mainKey]
}];
} else {
acc[key1] = obj[key1];
}
return acc;
}, {})
const finalResult = { ...obj,
...res
};
console.log(finalResult)
答案 5 :(得分:0)
所以这段代码可以做到,但是我很困惑为什么要使用这种格式。似乎让1stDomestic,1stEmployee和1stIndependent成为自己的对象而不是单个元素数组更有意义。只是需要您以后做更多的工作才能访问数据!
var requestedData = {
UserId: "",
BidderId: "",
"lstDomestic.BidderId" : "",
"lstDomestic.UserId":"234",
"lstEmployee.CreatedDate":"",
"lstIndependent.UpdatedDate":"",
"lstIndependent.CreateDate":""
}
let output = {};
for (let prop in requestedData) {
// Check to see if this is a "blah.thing" property
if (prop.includes(".")) {
let props = prop.split(".");
// Check to see if we've added the array already
if (output[props[0]])
output[props[0]][0][props[1]] = requestedData[prop];
else
// ES 2015 baby!
output[props[0]] = [{[props[1]]: requestedData[prop]}]
}
// If it's not then just add it normally
else
output[prop] = requestedData[prop];
}
console.log(output);