After checking out this post on flattening arrays, I noticed that no one had used the array method forEach. I gave it a try and failed, only receiving back an empty array:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach( (element) => {
result.concat(element)
})
console.log(result) //[]
Where did I go wrong?
You have to result = result.concat(element)
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.
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach((element) => {
result = result.concat(element)
})
console.log(result) //[]
Doc: concat
.concat() always returns a new array. It doesn't modify arrays on which it is operating.
You are supposing that .concat() will save result in its operand after completing its opeartion. However it is not the case. You need to save result explicitly after .concat():
result = result.concat(element);
Demo:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach((element) => {
result = result.concat(element)
});
console.log(result);
You can also use spread syntax instead of .forEach() to flatten array.
let result = [].concat(...arrays);
Demo:
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [].concat(...arrays);
console.log(result);
concat returns a new array and hence you need to assign it to result like result = result.concat(element)
let arrays = [[1, 2, 3], [4, 5], [6]];
let result = [];
arrays.forEach( (element) => {
result = result.concat(element)
})
console.log(result)
Related
I have an array of products amount, and another array of products ID's.
I'm trying to do something like this:
arrayOfAmount = [2,3,4]
arrayOfId = [1,2,3]
resulting the third array of ID's:
arrayOfProducts = [1,1,2,2,2,3,3,3,3]
how can I do that?
thank you in advance!
You can try this:
let arrayOfAmount = [2,3,4]
let arrayOfId = [1,2,3]
console.log(arrayOfId.flatMap((x,i)=>Array(arrayOfAmount[i]).fill(x)))
Here is a version how you could do this using the map and flat and fill methods of arrays. I added some comments to help understanding what the code does:
const arrayOfAmount = [2, 3, 4];
const arrayOfId = [1, 2, 3];
const arrayOfProducts = arrayOfId.map((id, index) => {
// get how often an id should be in the array
const length = arrayOfAmount[index];
// create an array with the length
const array = Array(length);
// insert the ID to every index of the array
return array.fill(id);
})
// and finally flat, so that the array doesn't look like: [[1, 1], [2, 2, 2], [3, 3, 3, 3]]
.flat();
console.log(arrayOfProducts);
A short version would be:
const arrayOfAmount = [2, 3, 4];
const arrayOfId = [1, 2, 3];
const arrayOfProducts = arrayOfId.map((id, index) => Array(arrayOfAmount[index]).fill(id))
.flat();
I want delete duplicate array from two arrays, but just show one of array, how I can do it ?
I want result
[1, 4]
const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 5, 6]
function arrayUniq(arr1, arr2) {
enter code here
}
As #Titus said, just filter the array comparing if one of them have repeated values.
const arr1 = [1, 2, 3, 4];
const arr2 = [2, 3, 5, 6];
function arrayUniq(arr1, arr2) {
const arrays = [...arr1,...arr2]
return arrays.filter(a=> !arr2.includes(a))
}
console.log(arrayUniq(arr1,arr2))
// remove duplicates from arr1 and arr2
function arrayUniq(arr1, arr2) {
let result = [];
// Find unique elements in arr1 & push them into result
arr1.forEach((e) => (arr2.find((f) => f === e) ? null : result.push(e)));
// Find unique elements in arr2 & push them into result
arr2.forEach((e) => (arr1.find((f) => f === e) ? null : result.push(e)));
return result;
}
console.log(arrayUniq(arr1, arr2));
I have a nested array
const folder = [1, [2, [3, [4, [5, [6]]]]]];
please use the basic concept of javaScript.
I need return like 123456. what should I do?
i tried using for loops but it didn't work.
use the recursive function here.
find the below solution
const folder = [1, [2, [3, [4, [5, [6]]]]]];
let result = '';
function convert(arr) {
arr.forEach((val) => {
if (Array.isArray(val)) {
convert(val);
} else {
result = result + val
}
})
return result
}
console.log(convert(folder))
I used another (my) way to store the elements of some array in another as a spread method.
I used join method for this way, but the array contains only one.
Here's my code:
const arr = [1, 2, 3];
const newArray = [eval(arr.join(', ')), 4, 5]
console.log(newArray) // [3, 4, 5]
Try this:
const arr = [1, 2, 3];
const newArray = [...arr, 4, 5];
console.log(newArray);
You can use concat for that.
The concat() method will join two (or more) arrays and will not change the existing arrays, but instead will return a new array, containing the values of the joined arrays.
const arr = [1, 2, 3];
const newArray = arr.concat([4, 5]);
console.log(newArray)
Another option is to use spread syntax (...) (introduced in ES6)
const arr = [1, 2, 3];
const newArray = [...arr, ...[4, 5]];
console.log(newArray)
I want to get values from an array of arrays, and I'm having difficulties doing it.
I have the following:
var id = 1; //value I want to use for the search
var _restrictions = [[1, 2], [2, 4], [5, 1], [1, 6]]; //arrays that I want to check
var arrVal = [];
By using the id, I want to retrieve all of the values, inside the arrays, where the id exits and store them in the array "arrVal".
For example:
_restrictions = [[1, 2], [2, 4], [5, 1], [1, 6]];
//arrVal will be: [2, 5, 6], because the id existing inside the arrays [1,2],
//[5,1] and [1,6]
The "_restrictions" array is a array of arrays that contain restrictions. They are independent values (the first one isn't the index or id).
How can I do that?
Thanks in advance!
Here's a version that will work for any size of nested array. It returns an flattened array of all values not including the id.
var id = 1;
var _restrictions = [[1, 2, 9], [2, 4], [5, 1], [1, 6]];
var arrVal = _restrictions.reduce((acc, c) => {
// Find the index of the id in each nested array
const i = c.findIndex(arr => arr === id);
// If it exists, splice it out and add it
// to the accumulator array
if (i > -1) {
c.splice(i, 1);
acc.push(...c);
}
return acc;
}, []);
console.log(arrVal);
EDIT: Updated code after the question is edited.
The question lacks a bit of clarity. I am assuming you want to filter the sub-arrays which have id in them i.e. contains the value 1.
let id = 1; //value I want to use for the search
let _restrictions = [[1, 2], [2, 4], [5, 1], [1, 6]];
let arrVal = _restrictions.filter((item) => {
return item.includes(id);
});
let new_array = arrVal.concat.apply([], arrVal).filter(x => x !== id);
console.log(new_array);
// [2, 5, 6]