If I have this JSON data that is from the original file.
[{"food": {"spicy": "spicy","sweet": "sweet"},
"water": {"sweet": "sweet","sour": {"cold": "ice lemon","hot": "lemon tea"}}
}, {"food": {"spicy": "spicy","sweet": "sweet"},
"water": {"sweet": "sweet","sour": {"cold": "ice lemon","hot": "lemon tea"}}
}]
how do I convert into a one-dimensional list like this?
[{
"food-spicy": "spicy",
"food-sweet ": "sweet",
"water-sweet ": "sweet",
"water-sour-cold ": "ice lemon",
"water-sour-hot ": "lemon tea"
}, {
"food-spicy": "spicy",
"food-sweet ": "sweet",
"water-sweet ": "sweet",
"water-sour-cold ": "ice lemon",
"water-sour-hot ": "lemon tea"
}]
Please help.
答案 0 :(得分:2)
您可以编写一个递归展平对象的函数。然后使用该函数map
数组:
function flatten(oldObj) {
return Object.keys(oldObj).reduce(function(newObj, oldKey) { // for each key "oldKey" in oldObj
var value = oldObj[oldKey]; // get the value of the current oldKey from oldObj
if(value && typeof(value) === "object") { // if the value is also an object (add "&& !Array.isArray(value)" to exclude arrays)
value = flatten(value); // then flatten it first
Object.keys(value).forEach(function(key) { // and for each key in the new flattened value object
newObj[oldKey + "-" + key] = value[key]; // add a key-value to newObj where key is "oldKey-key" and the value is value[key]
});
} else {
newObj[oldKey] = value; // otherwise (if the value is not an object), just copy the key-value to newObj as it is
}
return newObj;
}, {});
}
然后像这样使用它:
var newArray = oldArray.map(flatten);
示例:强>
function flatten(oldObj) {
return Object.keys(oldObj).reduce(function(newObj, oldKey) {
var value = oldObj[oldKey];
if(value && typeof(value) === "object") {
value = flatten(value);
Object.keys(value).forEach(function(key) {
newObj[oldKey + "-" + key] = value[key];
});
} else {
newObj[oldKey] = value;
}
return newObj;
}, {});
}
var oldArray = [{"food":{"spicy":"spicy","sweet":"sweet"},"water":{"sweet":"sweet","sour":{"cold":"ice lemon","hot":"lemon tea"}}},{"food":{"spicy":"spicy","sweet":"sweet"},"water":{"sweet":"sweet","sour":{"cold":"ice lemon","hot":"lemon tea"}}}];
var newArray = oldArray.map(flatten);
console.log(newArray);

答案 1 :(得分:2)
试试这个:
var data = [{
food: {
spicy: 'spicy',
sweet: 'sweet'
},
water: {
sweet: 'sweet',
sour: {
cold: 'ice lemon',
hot: 'lemon tea'
}
}
},
{
food: {
spicy: 'spicy',
sweet: 'sweet'
},
water: {
sweet: 'sweet',
sour: {
cold: 'ice lemon',
hot: 'lemon tea'
}
}
}
]
var res = data.map(obj => {
return Reflect.ownKeys(obj).reduce((re, key) => {
Reflect.ownKeys(obj[key]).reduce((r, k) => {
if (typeof obj[key][k] === 'object') {
Reflect.ownKeys(obj[key][k]).reduce((r2, k2) => {
r2[key + '-' + k + '-' + k2] = obj[key][k][k2]
return r2
}, r)
} else {
r[key + '-' + k] = obj[key][k]
return r
}
}, re)
return re
}, {})
})
console.log(res)
这是一个更通用的版本,适用于更多嵌套的obj
var data = [{
food: {
spicy: 'spicy',
sweet: 'sweet'
},
water: {
sweet: 'sweet',
sour: {
cold: 'ice lemon',
hot: 'lemon tea'
}
}
},
{
food: {
spicy: 'spicy',
sweet: 'sweet'
},
water: {
sweet: 'sweet',
sour: {
cold: 'ice lemon',
hot: 'lemon tea'
}
}
}
]
function customReduce(obj, subObj, lastReduceObj = {}, lastKey = '') {
let res = Reflect.ownKeys(subObj).reduce((re, key) => {
if (lastKey === '') {
customReduce(obj, obj[key], re, key)
} else if (typeof subObj[key] === 'object') {
customReduce(obj, subObj[key], re, lastKey + '-' + key)
} else {
re[lastKey + '-' + key] = subObj[key]
}
return re
}, lastReduceObj)
return res
}
var res = data.map(obj => {
return customReduce(obj, obj)
})
console.log(res)
答案 2 :(得分:0)
试试这个:
var jsonObj = [{
"food": {
"spicy": "spicy",
"sweet": "sweet"
},
"water": {
"sweet": "sweet",
"sour": {
"cold": "ice lemon",
"hot": "lemon tea"
}
}
}, {
"food": {
"spicy": "spicy",
"sweet": "sweet"
},
"water": {
"sweet": "sweet",
"sour": {
"cold": "ice lemon",
"hot": "lemon tea"
}
}
}];
var newObj = {};
for (var i in jsonObj) {
for (var j in Object.keys(jsonObj[i])) {
for (var k in Object.keys(jsonObj[i])[j]) {
newObj[Object.keys(jsonObj[i])[j]+'-'+Object.keys(jsonObj[i][Object.keys(jsonObj[i])[j]])[k]] = jsonObj[i][Object.keys(jsonObj[i])[j]][Object.keys(jsonObj[i][Object.keys(jsonObj[i])[j]])[k]];
}
}
}
console.log([flatJSON(newObj)]);
function flatJSON(obj) {
if(typeof obj != 'object') {
return;
}
for (var l in Object.keys(obj)) {
if (typeof obj[Object.keys(obj)[l]] == 'object') {
for (var m in Object.keys(obj[Object.keys(obj)[l]])) {
newObj[Object.keys(obj)[l]+'-'+Object.keys(obj[Object.keys(obj)[l]])[m]] = obj[Object.keys(obj)[l]][Object.keys(obj[Object.keys(obj)[l]])[m]];
}
}
if (typeof obj[Object.keys(obj)[l]] == 'object') {
delete obj[Object.keys(obj)[l]];
}
if (obj[Object.keys(obj)[l]] === undefined) {
delete obj[Object.keys(obj)[l]];
}
}
return newObj;
}