简体   繁体   English

创建包含对象的新数组

[英]Create new array containing objects

I wish to create a new array that contains objects from another Object 我希望创建一个包含来自另一个Object的对象的新数组

This was my try: 这是我的尝试:

var obj =  {
    a:{},
    b:{}
}

var arr = new Array().concat(obj,[]);

Sadly, this is returning an array like this: 可悲的是,这是返回一个这样的数组:

Array[1]
0: Object
    a: Object
    b: Object

The desired array, however, should look like this: 但是,所需的数组应如下所示:

Array[2]
    0: Object
        a: Object
    1: Object
        b: Object

How could i achieve that in the shortest possible way, without having to loop the object? 我怎么能以最短的方式实现这一点,而不必循环对象?

Here is an example without explicit loops: 这是一个没有显式循环的例子:

var r = Object.keys(obj).map(function(key) {
    var o = {};
    o[key] = this[key];
    return o;
}, obj);

http://jsfiddle.net/zNh3G/ http://jsfiddle.net/zNh3G/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从包含数组属性的基础对象中有选择地创建新的对象数组 - Selectively create a new array of objects from a base object containing array properties 遍历对象数组,找到匹配值,然后创建包含每个匹配值集的新数组 - Cycle through array of objects, find matching value and then create new array containing each set of matched values 如何使用 javascript 中的价格和数量之和创建包含唯一对象的新对象数组 - How to create a new array of objects containing the unique, with the sum of prices and quantity in javascript 从JSON对象数组文件中创建一个新的JSON字符串-不包含不包含img URL的对象 - Create a new JSON string out of a JSON object array file - without the objects that do not containing img URL 将对象数组中包含相同 id 的对象放入新数组中 - Get objects containing same id in array of objects into new array 从对象创建新数组 - Create new array from objects 比较2个对象以在javascript中创建对象的新数组 - Compare 2 objects to create a new array of objects in javascript JS用新密钥创建一个新的对象数组 - JS create a new array of objects with new key 如何从对象数组中创建一个新数组? - How to create a new array out of array of objects? 解析嵌套的数组对象,并创建一个新的数组 - Parse a nested array objects, and create a new array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM