This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 6 years ago.
I am new in JavasSript.
[Array[1], Array[2], Array[0], Array[4], Array[2], Array[8], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1], Array[1]]
I have this array of objects. I want to combine all array objects to single array of objects.
How can i do this?
You could use a combination of reduce and concat on your array of arrays
var arrOfArrays = [["a","b"],["C","D"]];
var flattened = arrOfArrays.reduce(function(p,c){
return p.concat(c);
});
console.log(flattened);
You can use concat and reduce
let list = [
[1,2,3],
[4,5,6],
[7,8,9]
];
const concat = (x,y) => x.concat(y);
let result = list.reduce(concat, []);
console.log(result);
// [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
var myArray = [[1, 4], [2, 4], [3, 4]];
var result = [].concat.apply([], myArray);
console.log(result); // 1, 4, 2, 4, 3, 4
Related
I have next code
function doneOrNot(board) {
console.log(board);
let blockNum = [...(board.splice(0, 2))];
return blockNum;
}
console.log(doneOrNot([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
On first console.log I got
(3) [Array(3), Array(3), Array(3)]
0: (3) [7, 8, 9]
length: 1
[[Prototype]]: Array(0)
(Length 3 or 1???)
And on second:
(2) [Array(3), Array(3)]
0: (3) [1, 2, 3]
1: (3) [4, 5, 6]
length: 2
[[Prototype]]: Array(0)
But why array of arrays have changed before (after?) split?
Array splice method, mutates original array and returns removed items.
const arr = [1, 2, 3];
// now arr has 3 elements
const removed_items = arr.splice(0, 2);
// Now removed_items will have [1, 2]
// and arr will have [3]
By the time When you expand on 'console log' in dev tools, the arr has only 1 element. That is reason you are seeing the 1 element.
This question already has answers here:
Convert array of objects to array of values [closed]
(2 answers)
Closed 1 year ago.
I have the following array of objects:
const input = [
{ key1: 1, key2: 1, key3: 5 },
{ key1: 2, key2: 5, key3: 6 },
{ key1: 4, key2: 6, key3: 7 },
{ key1: 7, key2: 8, key3: 9 },
];
I just want to loop through an array of objects and collect all the values into a one dimensional array like this:
const output = [1, 1, 5, 2, 5, 6, 4, 6, 7, 7, 8, 9];
Can someone help solve this problem?
If the keys are always in that order, you can use Object.values as a callback to flatMap
const output = input.flatMap(Object.values)
const input = [
{ key1: 1, key2: 1, key3: 5 },
{ key1: 2, key2: 5, key3: 6 },
{ key1: 4, key2: 6, key3: 7 },
{ key1: 7, key2: 8, key3: 9 },
];
const output = input.flatMap(Object.values)
console.log(output)
Relying the order of keys in an object is generally NOT a good idea. You can destrcuture all the properties you need and flatten them in that order:
input.flatMap(({ key1, key2, key3 }) => [key1, key2, key3])
or
Create an array of keys to make it a bit more dynamic:
const keys = ["key1", "key2", "key3"]
const output = input.flatMap(o => keys.map(k => o[k]))
I have this array shown in console
(3) [{…}, {…}, {…}]
0: {num3: 1, num2: 1}
1: {num3: 2, num2: 4}
2: {num3: 3, num2: 1}
length: 3
pop: ƒ ()
push: ƒ ()
shift: ƒ ()
splice: ƒ ()
unshift: ƒ ()
_chartjs: {listeners: Array(1)}
__proto__: Array(0)
I just want to change the format to this
(3) [{…}, {…}, {…}]
0: {1, 1}
1: {2, 4}
2: {3, 1}
length: 3
pop: ƒ ()
push: ƒ ()
shift: ƒ ()
splice: ƒ ()
unshift: ƒ ()
_chartjs: {listeners: Array(1)}
__proto__: Array(0)
So then I can use it to draw a scatter chart using chart.js
You can use map function like so.
let newArr = oldArr.map(x => [x.num3, x.num2])
I think you mean to output an array of arrays considering an object must have a key/value. To do so, this function should get the job done.
const formatArr = (arr) => arr.map(({ num1, num2 }) => ([num1, num2]))
You can then use this for multiple charts like so:
const newChart = formatArr(oldChart)
FYI: You should probably rename my function to something more descriptive.
Since you want to remove the name, then you should turn to an object into an array of values.
you cant do this: item = {1,2} but you can do this item = [1,2].
Considering you meant removing the properties which will lead to an array [1,2], Try this:
const arr = [
{num3: 1, num2: 1},
{num3: 2, num2: 4},
{num3: 3, num2: 1}]
const arr2 = arr.map(k => Object.keys(k).map(ok => k[ok]))
console.log({arr2})
Update: Looking at the scatter chart in chart.js you should have an x and a y as props instead, use this:
const arr = [
{num3: 1, num2: 1},
{num3: 2, num2: 4},
{num3: 3, num2: 1}]
// your object should be like this
const data = arr.map(k => ({x: k.num3,y: k.num2}))
console.log(data)
Reference: Scatter Chart - Chart.js
I have an object like this:
obj = {'id': 1, a: [1, 2, 3]}
I want to destructure and get the array a from obj
arr = {...obj.a}
I get:
{0: 1, 1: 2, 2: 3}
which is not an array
How to get the array itself ?
You are spreading an array inside {}. This creates an object with indices of the array as keys. This is why you get {0: 1, 1: 2, 2: 3}
const a = [ 1, 2 ]
console.log({ ...a })
If you want to get a property into a variable, this is the correct syntax:
const { propertyName } = yourObject
// if you want to have a variable name which is different than the propertyName
const { propertyName: someOtherVariable } = yourObject
Here's the working snippet:
const obj = {'id': 1, a: [1, 2, 3] }
const { a: arr } = obj; // this is same as: const arr = obj.a
console.log(arr)
Almost - it's the other way round :)
let {a: arr} = {'id': 1, a: [1, 2, 3]}
You could destructure to an array by assigning the array to an array with a rest syntax.
var obj = { id: 1, a: [1, 2, 3] },
[...arr] = obj.a;
console.log(arr);
Use brackets instead of curly braces to spread it into a new array:
const obj = {'id': 1, a: [1, 2, 3]}
const arr = [...obj.a]
console.log(arr)
I have an array-like object:
[1:Array[10], 2: Array[2], 3: Array[2], 4: Array[2], 5: Array[3], 6: Array[1]]
Im trying to remove the first two elements, do some stuff, and then insert them again at the same place.
Here is what i do:
array = Array.prototype.splice.call(array, 0,2);
When logging array in firefox it show this:
Array [ <2 empty slots>, Array[1], Array[2], Array[3], Array[1] ]
Looks good to me,I removed the first two elements and the array now starts with 2 empty slots.
So now, what I hope to do is to add objects to these 2 empty slots.
For simplicity, lets remove to items from the array and then insert them again at the same place:
var twoFirstItems = Array.prototype.slice.call(array, 0,2);
array = Array.prototype.splice.call(array, 0,2);
Now,to re-insert twoFirstItems I would think that I could do:
array.unshift(twoFirstItems)
This does not work as expected, it inserts the array but it does not have a key as it had before its modifikation. I assume this has to do with unshift not working the same with an array-like object as with an array.
So how do you remove/insert elements to an array-like-object properly?
If i do the following:
console.log(array);
console.log(typeof array);
The result:
Array [ <1 empty slot>, Array[2], Array[2], Array[2], Array[3], Array[1] ]
object
Without any complications, you can just re-assign the modified array at that index.
var a = [
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6]
]
a[0] = a[0].map((el) => el * 10)
a[1] = a[1].map((el) => el * 20)
console.log(a)