使数组对象都具有相同的键
Make array objects all have the same keys
我有一个对象数组
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中是否有内置函数?
看起来你想要
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;
});
});
只添加有值的键,如果不存在:
$(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);
});
您可以使用 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 );
});
纯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;
}, {});
根据@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];
}
});
我有一个对象数组
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中是否有内置函数?
看起来你想要
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;
});
});
只添加有值的键,如果不存在:
$(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);
});
您可以使用 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 );
});
纯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;
}, {});
根据@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];
}
});