[英]Make array objects all have the same keys
I have a array of object 我有一个对象数组
var arr =
[
{"shares":50,"comments":10},
{"likes":40,"shares":30},
{"comments":10}
];
I want to convert it to 我想将其转换为
var arr =
[
{"shares":50,"comments":10,"likes":0},
{"likes":40,"shares":30,"comments":0},
{"comments":10,"likes":0,"shares":0}
]
Properties are not fixed numbers and names would be different see another example 属性不是固定数字,名称会有所不同,请参见另一个示例
var arr2 = [{"a":1},{"b":2},{"c":3},{"d":4},{"e":5},{"f":6}]
to 至
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}]
I can do by iterating all elements of array and keys of every element but I don't know that, is it best way? 我可以通过迭代数组的所有元素和每个元素的键来完成操作,但是我不知道,这是最好的方法吗?
Is there any inbuilt function in JavaScript or jQuery? JavaScript或jQuery中是否有内置函数?
You can use http://api.jquery.com/jquery.extend/ 您可以使用http://api.jquery.com/jquery.extend/
var defaults = { "shares" : 0, "comments" : 0, "likes" : 0 };
arr = $.map( arr, function( item ){
return $.extend( {}, defaults, item );
});
http://jsfiddle.net/L74q3ksw/ http://jsfiddle.net/L74q3ksw/
Edit re: updated question 重新编辑:更新的问题
Now it's a matter of building the defaults
object which, as I understand, has all the unique keys from all the elements in your array. 现在,要构建
defaults
对象,据我所知,该对象具有数组中所有元素的所有唯一键。
So, given 所以,给定
var arr2 = [{"a":1},{"b":2},{"c":3},{"d":4},{"e":5},{"f":6}]
you need to extract "a", "b", "c", "d"... etc and create the defaults
. 您需要提取“ 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 );
});
http://jsfiddle.net/7dtfzg2f/2/ http://jsfiddle.net/7dtfzg2f/2/
It looks like you want 看起来像你想要的
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;
});
});
Just add the key with value, if it not exists: 只需将键和值相加(如果不存在):
$(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);
});
With pure JavaScript: 使用纯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;
});
This keeps you original array as it was. 这将使您的原始数组保持原样。 If you want to change your original array you can do following:
如果要更改原始数组,可以执行以下操作:
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];
}
});
You can get the default values from the array with following snippet: 您可以使用以下代码片段从数组中获取默认值:
var defaults = arr.reduce(function(m,v) {
for (var key in v) {
m[key] = 0;
}
return m;
}, {});
Building on what @dusky said, if you don't actually know what columns will be coming in, you can make a list of columns as a string set. 以@dusky所说的为基础,如果您实际上不知道将要输入哪些列,则可以将列列表作为字符串集。 Also assumes you just want to edit the existing array, not make a new one, and use ES6.
还假设您只想编辑现有阵列,而不要创建一个新阵列,然后使用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];
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.