我有一个对象的输入数组和一个期望/输出数组
对象的输入数组
var input = [{
"Grade": "AU27",
"Thickness10Qty": "7.00",
"Thickness5Qty": "19.20",
"TotalQty": "26.20"
},
{
"Grade": "FE500D",
"Thickness10Qty": "143.00",
"Thickness12Qty": "69.00",
"Thickness8Qty": "30.00",
"TotalQty": "242.00"
},
{
"Grade": "GE500D",
"Thickness18Qty": "90.00",
"Thickness22Qty": "40.00",
"TotalQty": "130.00"
},
{
"Grade": "HE500D",
"Thickness26Qty": "70.00",
"TotalQty": "70.00"
}
];
我将此作为所需的对象数组
var output = [{
"Grade": "AU27",
"Thickness10Qty": "7.00",
"Thickness5Qty": "19.20",
"Thickness12Qty": "0.00",
"Thickness8Qty": "0.00",
"Thickness18Qty": "0.00",
"Thickness22Qty": "0.00",
"Thickness26Qty": "0.00",
"TotalQty": "26.20"
},
{
"Grade": "FE500D",
"Thickness10Qty": "143.00",
"Thickness12Qty": "69.00",
"Thickness8Qty": "30.00",
"Thickness5Qty": "0.00",
"Thickness18Qty": "0.00",
"Thickness22Qty": "0.00",
"Thickness26Qty": "0.00",
"TotalQty": "242.00"
},
{
"Grade": "GE500D",
"Thickness18Qty": "90.00",
"Thickness22Qty": "40.00",
"Thickness10Qty": "0.00",
"Thickness12Qty": "0.00",
"Thickness8Qty": "0.00",
"Thickness5Qty": "0.00",
"Thickness26Qty": "0.00",
"TotalQty": "130.00"
},
{
"Grade": "HE500D",
"Thickness26Qty": "70.00",
"Thickness18Qty": "0.00",
"Thickness22Qty": "0.00",
"Thickness10Qty": "0.00",
"Thickness12Qty": "0.00",
"Thickness8Qty": "0.00",
"Thickness5Qty": "0.00",
"TotalQty": "70.00"
}
];
我要做的是假设在对象输入数组的第一个对象中,只有Thickness10Qty和Thickness5Qty,因此我必须将其他键输入为0。我希望每个对象中的键数相等。类似地,对于对象输入数组中的最后一个对象,仅存在Thickness26Qty。因此,我们将其他键插入为0,以使每个对象中的键数量相等。
如何用相同数量的键实现所需的对象数组?
密钥不能被硬编码,它来自服务,服务可能返回Thickness34Qty / Thickness46Qty而不是Thickness22Qty或Thickness38Qty而不是Thickness18Qty
答案 0 :(得分:1)
对所有应包含在对象中的键进行数组排列,然后遍历对象数组,并添加所有缺少的键,其值为0.00
:
var input = [{
"Grade": "AU27",
"Thickness10Qty": "7.00",
"Thickness5Qty": "19.20",
"TotalQty": "26.20"
},
{
"Grade": "FE500D",
"Thickness10Qty": "143.00",
"Thickness12Qty": "69.00",
"Thickness8Qty": "30.00",
"TotalQty": "242.00"
},
{
"Grade": "GE500D",
"Thickness18Qty": "90.00",
"Thickness22Qty": "40.00",
"TotalQty": "130.00"
},
{
"Grade": "HE500D",
"Thickness26Qty": "70.00",
"TotalQty": "70.00"
}
];
var keys = ["Grade", "TotalQty", "Thickness5Qty", "Thickness8Qty", "Thickness10Qty", "Thickness12Qty", "Thickness18Qty", "Thickness22Qty", "Thickness26Qty"];
var output = input.map(obj => {
keys.forEach(key => obj[key] ? key : obj[key] = "0.00");
return obj;
});
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: auto; }
如果键是可变的(例如,您希望每个对象都具有相同的键):
var input = [{
"Grade": "AU27",
"Thickness10Qty": "7.00",
"Thickness5Qty": "19.20",
"TotalQty": "26.20"
},
{
"Grade": "FE500D",
"Thickness10Qty": "143.00",
"Thickness12Qty": "69.00",
"Thickness8Qty": "30.00",
"TotalQty": "242.00"
},
{
"Grade": "GE500D",
"Thickness18Qty": "90.00",
"Thickness22Qty": "40.00",
"TotalQty": "130.00"
},
{
"Grade": "HE500D",
"Thickness26Qty": "70.00",
"TotalQty": "70.00"
}
];
var keys = [...new Set(input.map(Object.keys).reduce((acc, curr) => acc.concat(curr)))];
var output = input.map(obj => {
keys.forEach(key => obj[key] ? key : obj[key] = "0.00");
return obj;
});
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: auto; }
答案 1 :(得分:1)
您可以创建一个模板对象,该模板对象的所有键均设置为0.00
,该键定义了所需的字段,然后可以在Object.assign()
内使用map()
将其与现有对象合并。
var input = [{"Grade": "AU27","Thickness10Qty": "7.00","Thickness5Qty": "19.20","TotalQty": "26.20"},{"Grade": "FE500D","Thickness10Qty": "143.00","Thickness12Qty": "69.00","Thickness8Qty": "30.00","TotalQty": "242.00"}, {"Grade": "GE500D","Thickness18Qty": "90.00","Thickness22Qty": "40.00","TotalQty": "130.00"}, {"Grade": "HE500D","Thickness26Qty": "70.00","TotalQty": "70.00"}];
// make template of all keys
let template = input.reduce((obj, item) => (Object.keys(item).forEach(k => obj[k] = '0.00'), obj), {})
// apply to items:
let newArray = input.map(item => Object.assign({}, template, item))
console.log(newArray)
答案 2 :(得分:0)
这可能不会设置任何性能记录,但是应该演示发生的步骤以及如何对其进行优化。重要的是要注意,对对象进行操作将使 input 数组中的对象发生变异,因此没有输入/输出数组,因为这将需要克隆对象,并且工作量超出所需。
let array = getData()
normalizeData(array)
console.log('output:', array)
function normalizeData(array) {
let stored_keys = {}
array.forEach((obj, ndx, arr) => {
let new_keys = []
// create keys found in other objects
Object.keys(stored_keys).forEach(key => {
if (!(key in obj))
obj[key] = "0.00"
})
// detect new keys
Object.keys(obj).forEach(key => {
if (!stored_keys[key]) {
stored_keys[key] = true // track key
new_keys.push(key) // track new keys to apply to previous objects
}
})
// add any new keys to previous objects
if (new_keys.length)
for (var i = 0, n = ndx; i < n; i++) {
let prev_obj = arr[i];
new_keys.forEach(key => prev_obj[key] = "0.00")
}
})
}
function getData() {
return [{
"Grade": "AU27",
"Thickness10Qty": "7.00",
"Thickness5Qty": "19.20",
"TotalQty": "26.20"
},
{
"Grade": "FE500D",
"Thickness10Qty": "143.00",
"Thickness12Qty": "69.00",
"Thickness8Qty": "30.00",
"TotalQty": "242.00"
},
{
"Grade": "GE500D",
"Thickness18Qty": "90.00",
"Thickness22Qty": "40.00",
"TotalQty": "130.00"
},
{
"Grade": "HE500D",
"Thickness26Qty": "70.00",
"TotalQty": "70.00"
}
];
}
答案 3 :(得分:0)
我认为@ mark-meyer与此有关。我将提供一个更具可读性的版本。
const baseObject = {
"Thickness10Qty": "0.00",
"Thickness5Qty": "0.00",
"Thickness12Qty": "0.00",
"Thickness8Qty": "0.00",
"Thickness18Qty": "0.00",
"Thickness22Qty": "0.00",
"Thickness26Qty": "0.00",
"TotalQty": "0.00"
};
const addMissingvalues = arr => arr.map(obj => ({
...baseObject,
...obj,
}));