我有一个对象数组
var arr =
[
{"shares":50,"comments":10},
{"likes":40,"shares":30},
{"comments":10}
];
我想将其转换为
var arr =
[
{"shares":50,"comments":10,"likes":0},
{"likes":40,"shares":30,"comments":0},
{"comments":10,"likes":0,"shares":0}
]
属性不是固定数字,名称会有所不同,请参阅另一个示例
var arr2 = [{"a":1},{"b":2},{"c":3},{"d":4},{"e":5},{"f":6}]
到
var arr2 = [{"a":1,"b":0,"c":0,"d":0,"e":0,"f":0},{"a":0,"b":1,"c":0,"d":0,"e":0,"f":0},{"a":0,"b":0,"c":1,"d":0,"e":0,"f":0},{"a":0,"b":0,"c":0,"d":1,"e":0,"f":0},{"a":0,"b":0,"c":0,"d":0,"e":1,"f":0},{"a":0,"b":0,"c":0,"d":0,"e":0,"f":1}]
我可以通过迭代每个元素的数组和键的所有元素来做但我不知道,这是最好的方法吗?
JavaScript或jQuery中是否有任何内置函数?
答案 0 :(得分:6)
您可以使用http://api.jquery.com/jquery.extend/
var defaults = { "shares" : 0, "comments" : 0, "likes" : 0 };
arr = $.map( arr, function( item ){
return $.extend( {}, defaults, item );
});
编辑重新:更新的问题
现在需要构建defaults
对象,据我所知,该对象具有数组中所有元素的所有唯一键。
所以,给定
var arr2 = [{"a":1},{"b":2},{"c":3},{"d":4},{"e":5},{"f":6}]
你需要提取“a”,“b”,“c”,“d”等等并创建defaults
。
var defaults = {};
// collect all the keys and set them with value = 0 in defaults
arr2.forEach( function( item ){
Object.keys( item ).forEach( function( key ){
defaults[ key ] = 0;
});
});
// same as the previous solution
arr2 = $.map( arr2, function( item ){
return $.extend( {}, defaults, item );
});
答案 1 :(得分:1)
看起来你想要
var set = {}; // it will be cleaner with ES6 Set
$.each(arr, function(_,e){
for (var k in e) set[k] = 1;
});
$.each(set, function(k){
$.each(arr, function(_,e){
if (e[k]===undefined) e[k] = 0;
});
});
答案 2 :(得分:0)
只需添加带有值的键(如果不存在):
$(function() {
var arr = [{"shares":50,"comments":10},{"likes":40,"shares":30},{"comments":10}];
$.each(arr, function(idx, obj) {
if (typeof obj.shares === 'undefined') {
obj.shares = 0;
}
if (typeof obj.comments === 'undefined') {
obj.comments = 0;
}
if (typeof obj.likes === 'undefined') {
obj.likes = 0;
}
arr[idx] = obj;
});
console.log(arr);
});
答案 3 :(得分:0)
使用纯JavaScript:
var defaults = { "shares": 0, "comments": 0, "likes": 0 };
var arr = [{"shares":50,"comments":10},{"likes":40,"shares":30},{"comments":10}];
arr.slice().map(function(e) {
for (var key in defaults) {
e[key] = e[key] || defaults[key];
}
return e;
});
这样可以保持原始阵列的原样。如果您想更改原始阵列,可以执行以下操作:
var defaults = { "shares": 0, "comments": 0, "likes": 0 };
var arr = [{"shares":50,"comments":10},{"likes":40,"shares":30},{"comments":10}];
arr.forEach(function(e) {
for (var key in defaults) {
e[key] = e[key] || defaults[key];
}
});
您可以使用以下代码段从数组中获取默认值:
var defaults = arr.reduce(function(m,v) {
for (var key in v) {
m[key] = 0;
}
return m;
}, {});
答案 4 :(得分:0)
基于@dusky所说的内容,如果您实际上不知道将要输入哪些列,则可以将列列表作为字符串集进行创建。还假设您只想编辑现有数组,而不要创建一个新数组,然后使用ES6。
// get all keys added to a list that cannot take duplicates
const columnNames = new Set<string>();
arr.forEach(element => {
Object.keys(element)
.forEach(x => columnNames.add(x));
});
// create default with nulls, can replace with 0 or anything here
const defaultKeys = {};
columnNames.forEach(x => {
defaultKeys[x] = null;
});
// Add all missing keys into each object
arr.forEach((element) => {
for (const key in defaultKeys) {
element[key] = element[key] || defaultKeys[key];
}
});