0

I have an object with the following values:

"Obj": {
    "value1": [],
    "value2": [
        "1",
        "2",
        "3",
        "4"
    ]
}

If I check my obj I can see that its size is equal to 2. That makes sense, of course, 'cause I have two arrays here. But the thing is, I need it to have one array only. By that I mean: I want to convert those 2 arrays into one array.

Have been trying for a while with a few methods. None of them had the expected behavior.

For example:

var arr = [];
arr = [sRows.value1, sRows.value2]

Or:

$.each(sRows, function(k,v){
    result[i] = v;
})

Any ideas?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Jose Peres
  • 197
  • 1
  • 1
  • 18
  • Not sure how you want to combine your arrays (you don't say), but my glass ball is telling me [`Array.prototype.concat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) may be what you are searching for. – ASDFGerte Jun 14 '18 at 17:32
  • Are there always just going to be 2 values? Are those values always going to be arrays? How do you want the conversion to be (value1 followed by value2, vice versa, no duplicates....)? – Matt Jun 14 '18 at 17:32
  • Always two arrays, not specific order. And yes, no duplicates. – Jose Peres Jun 14 '18 at 17:45
  • 2
    Possible duplicate of [How to merge two arrays in JavaScript and de-duplicate items](https://stackoverflow.com/questions/1584370/how-to-merge-two-arrays-in-javascript-and-de-duplicate-items) – Asons Jun 14 '18 at 17:52
  • In your example above, what is the correctly value you expect as a size for your `Obj` ? 5? or 4? – Ramy Nasr Jun 14 '18 at 17:58
  • I expect a length of "1". – Jose Peres Jun 14 '18 at 17:59
  • Given the information you gave us, both mine and Jonas' answer will work. Are you sure your expectation of what an empty array or empty object look like match reality? – Caleb Jay Jun 14 '18 at 20:55

2 Answers2

0

How about Array.prototype.concat() ?

let new_array = sRows.value1.concat(sRows.value2);

The concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Caleb Jay
  • 2,159
  • 3
  • 32
  • 66
  • Can't use concat 'cause the value of the first array is an empty object. – Jose Peres Jun 14 '18 at 17:36
  • That shouldn't matter: https://repl.it/@calebjay/BlueNeatQuark Can you clarify what you mean? – Caleb Jay Jun 14 '18 at 17:38
  • Sure. When I do it like this, the resulting value's: (4) ["1", "2", "3", "4"]. So it creates 4 different elements with every value of "value1" and ignores "value2". Not working as expected, then. – Jose Peres Jun 14 '18 at 17:43
  • In your example, value1 is an empty array. The empty array has nothing to copy, so nothing is copied. How does this differ from your expectation? (I'm assuming in your comment you accidentally wrote value2 when you meant value1) – Caleb Jay Jun 14 '18 at 17:44
  • Yes, sorry. That's what I meant. The actual problem's what I pointed out first: "It's returning an array.length of "4". – Jose Peres Jun 14 '18 at 17:48
  • It is correct. Try this: do `console.log(sRows.value1.length + sRows.value2.length)` and see if that matches your expectations. – Caleb Jay Jun 14 '18 at 17:50
  • Even better: `console.log((sRows.value1.length + sRows.value2.length) === sRows.value1.concat(sRows.value2).length)` – Caleb Jay Jun 14 '18 at 17:51
  • This is also an option sRows.value1.push( ...sRows.value2 ) or sRows.value2.unshift( ...sRows.value1 ) to make it for opposit – Kishore Chandra Jun 14 '18 at 18:05
0

This function will convert your object into a single array:

function convertArraysToArray(obj) {
    var output = [];
    for (var key in obj) {
        if (Array.isArray(obj[key])) {
            for (var index = 0; index < obj[key].length; index++) {
                output.push(obj[key][index]);
            }
        } else {
            output.push(obj[key]);
        }
    }
    return output;
}

If you intend to make it a single element of an object, you can achieve that easily as well.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175