1

I have 2 arrays:

var keys = Object.keys(item); // [first, second, third, forth]
var values = Object.values(item); // [fifth, sixth, seventh, eighth]
let newArray = [];

output should be:

[first, fifth, second, sixth, third, seventh, forth, eighth]

is it possible without using any while, for loops like for i in item or for i = 0; i <= item.length; i++..
Just with using map, splice etc.

  • Does this answer your question? [map function for objects (instead of arrays)](https://stackoverflow.com/questions/14810506/map-function-for-objects-instead-of-arrays) – Federico klez Culloca Dec 04 '19 at 13:13
  • no man.. they are different – Daniyar Changylov Dec 04 '19 at 13:16
  • @Daniyar If you're actually trying to make an array like `[key, value, key, value]` starting from an object, then that link should answer your question. If you want to do this in the general case of two arrays then please remove the part of your question about keys and values because it muddies the water. – Federico klez Culloca Dec 04 '19 at 13:19

3 Answers3

8

You could simply do:

Object.entries(item).flat()

Your expected output is

[key1, value1, key2, value2,...]

Object.entries() returns a 2-dimensional array like this: [[key1, value1], [key2, value2],...]. You can use flat on this array to create an array of alternate key-value items

Here's a snippet:

let item = {
  first: "fifth",
  second: "sixth",
  third: "seventh",
  forth: "eighth"
};

const output = Object.entries(item).flat()

console.log(output)
adiga
  • 34,372
  • 9
  • 61
  • 83
  • 1
    You've beat me at code golf! I wrote a `.map` function which just essentially does `Object.entries` for my answer – TKoL Dec 04 '19 at 13:17
  • what if its a nested dict? – yogesh mhetre Dec 04 '19 at 13:32
  • @yogeshmhetre what would the output look like? According to this code, it would still be a key, value chain but the value will be object. OP hasn't mentioned how to handle that. – adiga Dec 04 '19 at 13:35
1

You can try with Array.prototype.forEach()

var keys = ['first', 'second', 'third', 'forth']
var values = ['fifth', 'sixth', 'seventh', 'eighth']
let newArray = [];
keys.forEach(function(el, i){
  newArray.push(el);
  if(values.length > i) newArray.push(values[i]);
});
console.log(newArray);

Update: Noticed the keys and values lately, in which case undoubtedly the best option is Object.entries():

The Object.entries() method returns an array of a given object's own enumerable string-keyed property [key, value] pairs, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well). The order of the array returned by Object.entries() does not depend on how an object is defined.

And Array.prototype.flat()

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

var arr = {'first':'fifth', 'second':'sixth', 'third':'seventh', 'forth':'eighth'};
let newArray = Object.entries(arr).flat();
console.log(newArray);
Andreas
  • 21,535
  • 7
  • 47
  • 56
Mamun
  • 66,969
  • 9
  • 47
  • 59
  • The first index of an array is `0` not `1` – Andreas Dec 04 '19 at 13:12
  • 1
    The [tag:functional-programming] tag suggests that the user is looking for a solution drawing on functional programming principles, which I do not think is reflected here: the `forEach` acts on variables outside of its scope and it mutates data. Looking at your edit history, the `entries() + flat()` suggestion is exactly what @adiga suggested before, so I'm not sure how this adds anything substantial. – customcommander Dec 04 '19 at 14:10
0
var item = {first:'fifth',second:'sixth',third:'seventh'};
var arr = Object.keys(item).map(k => [k, item[k]]).flat();
console.log(arr);

First I make a 2d array, [[first,fifth],[second,sixth]], and then I flatten it with .flat()

TKoL
  • 13,158
  • 3
  • 39
  • 73