How to remove [ ] inside another array?

Hello, I’m trying to remove [ ] from result of array split.

The output value should be [{name: "something"}, {......},"
But my code is creating [{name: ["something"]}, {.....},"

var ring = ["GER", "FRA", "SPA", "BEL", "NET", "IRE"];

var arrays = [], size = 1;
    
while (ring.length > 0)
  arrays.push(ring.splice(0, size));
  
var comb = arrays.map(function combine(dataItem) {
	return {
  	name: dataItem
}});

console.log(comb);

JSFIDDLE:

This is what I mean:

Probably I’m messing up with basic preformat, but I can’t figure out where, or just if I need to add something else after the split ?

Thanks!

splice returns an array.

you… could, reduce this code to…
comb = ring.map((x) => { return {"name":x}; });
? (Dunno if you need arrays for something else.)

1 Like

That worked exactly as I wanted but how? and why?
I needed to splice ring in order to get [ value] , [value] separated, then map them to return each one of them in the formatted order

You have an array of strings (ring).
Map says “Make a copy of this array. Take each of the things in the array, and replace them with the result of my function.”
The function just says: “Take what was given (the string), stuff it into an object property called “name”, and return the object.”

So you end up with an array of the returned things. Which are objects.

1 Like

So basically I was just involving unnecessary stuff :sweat_smile:

Thanks for the help and the explanation @m_hutley

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.