简体   繁体   English

如何将对象的每个值放入数组

[英]How to place each value of an object into an array

I have two objects:我有两个对象:

a = {A: 1, B: 2};
a = {C: 3, D: 4};

I need to put the property values of the objects in to one array.我需要将对象的属性值放入一个数组中。 So, i'm doing this by iterate each of the objects, but what next, pushing it into one array returns two separated...所以,我通过迭代每个对象来做到这一点,但是接下来,将它推入一个数组会返回两个分开的......

$.each(a, function(_key, _val) {
    var arr = [];
    arr.push(_val);
    console.log(arr);
});

How to do this?这该怎么做? Is it possible?是否可以? I need this: arr = [1, 2, 3, 4];我需要这个: arr = [1, 2, 3, 4];

You are initializing the arr inside the each everytime.您每次都在每个内部初始化 arr。 Try this:尝试这个:

var arr = [];
$.each(a, function(_key, _val) {
    arr.push(_val);
    console.log(arr);
});

Here is a fiddle:这是一个小提琴:

https://jsfiddle.net/aarLgmtL/ https://jsfiddle.net/aarLgmtL/

var a = {A: 1, B: 2};
var b = {C: 3, D: 4};

function join(source, target) {
    $.each(source, function(_key, _val) {
        target.push(_val);
    });
}

var arr = [];
join(a, arr);
join(b, arr);

$("#result").html(JSON.stringify(arr));

You could try using .concat as well.您也可以尝试使用 .concat。 Here is an example from w3schools:以下是来自 w3schools 的示例:

var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var children = hege.concat(stale);

http://www.w3schools.com/jsref/jsref_concat_array.asp http://www.w3schools.com/jsref/jsref_concat_array.asp

Is this what you are looking for?这是你想要的?

Don't initialize array in $.each function.不要在$.each函数中初始化数组。 It will assign new array every time $.each function call so it end up with last array element in arr variable.它会在每次 $.each 函数调用时分配新数组,因此它以arr变量中的最后一个数组元素结束。

 a = { A: 1, B: 2 }; b = { C: 3, D: 4 }; var arr = []; function pushToArray(a) { $.each(a, function(_key, _val) { arr.push(_val); }); } pushToArray(a); pushToArray(b); console.log(arr);
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Make sure that you don't define different object with same name else end up with last object assigned to that variable.确保您没有定义具有相同名称的不同对象,否则最后一个对象将分配给该变量。

The approach outlined below first creates two separate arrays, one from a and one from b , and then concatenates them together (with a little help from the jQuery Array#map method.)下面概述的方法首先创建两个单独的数组,一个来自a ,一个来自b ,然后将它们连接在一起(借助 jQuery Array#map方法的一点帮助。)

a = {A: 1, B: 2};
b = {C: 3, D: 4};

var arrayFromA = $.map(a, function(value, index) {
    return [value];
});

var arrayFromB = $.map(b, function(value, index) {
    return [value];
});

finalArray = arrayFromB.concat(arrayFromA);

You can simply achieve this by using Object.assign您可以通过使用 Object.assign 简单地实现这一点

    var a = {A: 1, B: 2};
    var b = {C: 3, D: 4};
    var copy = Object.assign(a, b);
    var result =[];
    for(var i in copy){
       result.push(copy[i]);
    }
console.log(result);

Thanks.谢谢。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM