Unflatten JSON响应

eni9jsuy  于 2023-03-24  发布在  其他
关注(0)|答案(4)|浏览(97)

我有这个JSON响应,我想将其解扁平化,但每个可能的解扁平化库都只是将其追加到数组中,我想突出显示3个对象,如图所示,有没有更简单的方法?

{
    "highlights.id": ["9ab80883-e8eb-4710-9818-e476fc32e356", "824ff2a0-1f17-44b7-b99f-ce227af12ea7", "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe", ],
    "highlights.key": ["0", "1", "2"],
    "highlights.value": ['16" Pedestal Fan', "High Quality Metal Grill ", "Heavy Duty Motor with 2000 RPM", ],
    "highlights.productId": ["655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", ],
    "highlights.createdAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ],
    "highlights.updatedAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ]
}

我想要的是
highlights:[ {// id, key, value, productId}, {}, {}]
但我越来越
highlights:{ id:[], productId:[], key:[], value:[]

eufgjt7s

eufgjt7s1#

使用Array.map

您可以使用.map函数循环通过键只是为了使其一致,并返回一个新的对象,您想要的方式,然后存储到新的突出显示数组。

演示

var jsonObject = { "highlights.id": [     "9ab80883-e8eb-4710-9818-e476fc32e356",     "824ff2a0-1f17-44b7-b99f-ce227af12ea7",     "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe",   ],   "highlights.key": ["0", "1", "2"],   "highlights.value": [     '16" Pedestal Fan',     "High Quality Metal Grill ",     "Heavy Duty Motor with 2000 RPM",   ],   "highlights.productId": [     "655ea6af-9e5c-401e-b317-ea1fac55857f",     "655ea6af-9e5c-401e-b317-ea1fac55857f",     "655ea6af-9e5c-401e-b317-ea1fac55857f",   ],   "highlights.createdAt": [     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",   ],   "highlights.updatedAt": [     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",   ] };

const highlights = jsonObject['highlights.key'].map((v, i) => {
    return {
        "id": jsonObject['highlights.id'][i],
        "key": jsonObject['highlights.key'][i],
        "value": jsonObject['highlights.value'][i],
        "productId": jsonObject['highlights.productId'][i],
        "createdAt": jsonObject['highlights.createdAt'][i],
        "updatedAt": jsonObject['highlights.updatedAt'][i],
    };
})
console.log(highlights);

动态解决方案:

如果希望它更动态,还可以循环JSON对象的键

var jsonObject = { "highlights.id": [     "9ab80883-e8eb-4710-9818-e476fc32e356",     "824ff2a0-1f17-44b7-b99f-ce227af12ea7",     "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe",   ],   "highlights.key": ["0", "1", "2"],   "highlights.value": [     '16" Pedestal Fan',     "High Quality Metal Grill ",     "Heavy Duty Motor with 2000 RPM",   ],   "highlights.productId": [     "655ea6af-9e5c-401e-b317-ea1fac55857f",     "655ea6af-9e5c-401e-b317-ea1fac55857f",     "655ea6af-9e5c-401e-b317-ea1fac55857f",   ],   "highlights.createdAt": [     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",   ],   "highlights.updatedAt": [     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",     "2023-03-15T07:27:39.150Z",   ] };
const highlights = jsonObject['highlights.key'].map((v, i) => {
    const newObject = {};
    Object.keys(jsonObject).forEach((v) => {
        newObject[v.replace('highlights.','')] = jsonObject[v][i];
    });
    return newObject;
})
console.log(highlights);
i86rm4rw

i86rm4rw2#

抱歉,无法转换为

highlights:[ {// id, key, value, productId}, {}, {}]

因为在你的数组highlights中有一个对象。你必须声明一个对象键。我有一个解决方案,使用Classconstructor

var object = {
  "highlights.id": ["9ab80883-e8eb-4710-9818-e476fc32e356", "824ff2a0-1f17-44b7-b99f-ce227af12ea7", "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe", ],
  "highlights.key": ["0", "1", "2"],
  "highlights.value": ['16" Pedestal Fan', "High Quality Metal Grill ", "Heavy Duty Motor with 2000 RPM", ],
  "highlights.productId": ["655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", ],
  "highlights.createdAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ],
  "highlights.updatedAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ]
}
var arr = []

class Highlights {
  constructor(id, key, value, productId, createdAt, updatedAt) {
    this.id = id;
    this.key = key;
    this.value = value;
    this.productId = productId;
    this.createdAt = createdAt;
    this.updatedAt = updatedAt
  }
}

for (i = 0; i < object["highlights.id"].length; i++) {
  let highlights = new Highlights(object["highlights.id"][i], object["highlights.key"][i], object["highlights.value"][i],
    object["highlights.productId"][i], object["highlights.createdAt"][i], object["highlights.updatedAt"][i])

  arr.push(highlights);
}
//get all 
console.log(arr)
//get arr 0 ID
console.log(arr[0].id);

如果你的意思是

highlights:[ {id:"a", key: "b", value: "c", productId: "d"}, {}, {}]

那我对你的解决办法就是...

var object = {
  "highlights.id": ["9ab80883-e8eb-4710-9818-e476fc32e356", "824ff2a0-1f17-44b7-b99f-ce227af12ea7", "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe", ],
  "highlights.key": ["0", "1", "2"],
  "highlights.value": ['16" Pedestal Fan', "High Quality Metal Grill ", "Heavy Duty Motor with 2000 RPM", ],
  "highlights.productId": ["655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", ],
  "highlights.createdAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ],
  "highlights.updatedAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ]
}
var arr = []
for (i = 0; i < object["highlights.id"].length; i++) {
  arr.push({
    id: object["highlights.id"][i],
    key: object["highlights.key"][i],
    value: object["highlights.value"][i],
    productId: object["highlights.productId"][i],
    createdAt: object["highlights.createdAt"][i],
    updatedAt: object["highlights.updatedAt"][i]
  });
}
//get all arr
console.log(arr)
//get arr 0 ID
console.log(arr[0].id)
//or
console.log(arr[0]["id"])

在这个用例中,类和对象之间的区别是命名约定,例如object允许您使用spacing for naming key,而您不能在constructor中这样做(_或大写键可以更容易区分)

xzv2uavs

xzv2uavs3#

注意:在javascript中更新对象时,[.](其中.是不带方括号的动态键)将成为静态键。

let flattenHighlights = []
let flattenKeysObject = {}
for(let key of Object.keys(highlights)){
     let highlightKey = key.replace('highlights.','')
     {...flattenKeysObject, [highlightKey]:highlights[key]}
}
console.log("highlights : ", flattenKeysObject)
blpfk2vs

blpfk2vs4#

你可以通过data数组迭代来实现。如果你想得到一个简单的答案,试试这个:
1.第一种方法:

let data = {
    "highlights.id": ["9ab80883-e8eb-4710-9818-e476fc32e356", "824ff2a0-1f17-44b7-b99f-ce227af12ea7", "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe", ],
    "highlights.key": ["0", "1", "2"],
    "highlights.value": ['16" Pedestal Fan', "High Quality Metal Grill ", "Heavy Duty Motor with 2000 RPM", ],
    "highlights.productId": ["655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", ],
    "highlights.createdAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ],
    "highlights.updatedAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ]
}

let newData = Array(data['highlights.id'].length).fill(null).map((d,index) => ({
  id: data['highlights.id'][index],
  key: data['highlights.key'][index],
  value: data['highlights.value'][index],
  productId: data['highlights.productId'][index],
//  createdAt: data['highlights.updatedAt'][index],
//  updatedAt: data['highlights.updatedAt'][index]
}));
console.log(newData);

1.第二种方法:

let data = {
    "highlights.id": ["9ab80883-e8eb-4710-9818-e476fc32e356", "824ff2a0-1f17-44b7-b99f-ce227af12ea7", "4a9e35c5-e03f-41d6-a4b5-635b08bc1bbe", ],
    "highlights.key": ["0", "1", "2"],
    "highlights.value": ['16" Pedestal Fan', "High Quality Metal Grill ", "Heavy Duty Motor with 2000 RPM", ],
    "highlights.productId": ["655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", "655ea6af-9e5c-401e-b317-ea1fac55857f", ],
    "highlights.createdAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ],
    "highlights.updatedAt": ["2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", "2023-03-15T07:27:39.150Z", ]
}

let newData = Array(data['highlights.id'].length).fill(null).map((d,index) => {
  let obj = {};
  Object.keys(data).forEach((key) => {
    obj[key.split('.')[1]] = data[key][index];
  });
  return obj;
}).map((d)=>{
  delete d['createdAt'];
  delete d['updatedAt'];
  return d;
});
console.log(newData);

相关问题