How to fill multidimensional array with empty arrays [duplicate] - javascript

This question already has answers here:
Unexpected behavior using Array Map on an Array Initialized with Array Fill [duplicate]
(1 answer)
Array.fill(Array) creates copies by references not by value [duplicate]
(3 answers)
Closed 25 days ago.
I am trying to initialize a two-dimensional array with empty arrays so I can add elements to them in a larger composition using Array.push. However, when I add to the inner arrays, they all get added to. Here is a simple example:
const arr = Array(3).fill([]);
arr[0].push(42);
Now arr is [[42],[42],[42]] but I was hoping for [[42],[],[]].
I think the problem is Array.fill is putting the same referenced empty array into each slot. How do I get fill to make a distinct empty array at each slot?

You can use Array#map.
const arr = [...Array(3)].map(_ => []);
arr[0].push(42);
console.log(arr);
Or Array.from.
const arr = Array.from({length: 3}, _ => []);
arr[0].push(42);
console.log(arr);

Related

How to manipulate an array, eliminating the empty items? [duplicate]

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
How to remove item from array by value? [duplicate]
(37 answers)
Closed 2 months ago.
I have an array which has some empty items.
const array = ["a","","c","","e","f","g"]
I am trying to eliminate empty items and leave only the items having string. I mean manipulating the array as:
array = ["a","c","e","f","g"]
There are many alternative like array.map, array.filter, array.slice, array.splice.. What is the most costless and recommended way of doing this?
As far as I know most cost effective way is to use the array filter method:
const array = ["a","","c","","e","f","g"];
const results = array.filter(element => {
return element !== '';
});
console.log(results);

push value in map of arrays at a specific postition [duplicate]

This question already has answers here:
How can I create a two dimensional array in JavaScript?
(56 answers)
Closed 5 years ago.
I am creating a map of array using this:
var m = new Map(Array(40).fill(new Array()).entries());
Now, I want to push values in those array. But when I do this:
m.get(1).push(10)
The value 10 gets pushed into all the arrays instead of the one at 1st position.
You could take another pattern to build independent arrays.
var m = new Map(Array.from({ length: 40 }, _=> []).entries());
m.get(1).push(10);
console.log([...m]);
fill gets single array an uses it to fill all rows of the given array, it doesn't create a new array for each row. This means that your single array reference is shared between all rows. Because array is a reference type, you use the single reference to manipulate it, so the actual object is changed. You can check this by comparing the references of each row.
const arr = new Array(2).fill(new Array());
console.log(arr[0] === arr[1]);
For creating separate arrays, you can see #Nina's answer above

Lodash / javascript : Compare two collections and return the differences [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 6 years ago.
I have two arrays of objects:
Elements of my tables are not primitive value, but complex objects.
array1 = [obj1,obj2,obj3,obj4]
array2 = [obj5,obj5,obj6,obj7]
I would like to compare two arrays and see if the elements of array2 are already present in array1 then create a new array of the difference.
Any suggestions?
var presents = _.intersectionWith(array1, array2, _.isEqual);
var dif = _.differenceWith(array1, array2, _.isEqual);
_.differenceWith is only available since 4.0.0 lodash version
ES6 This will be enough:
array2.filter(e => !array1.includes(e));
without includes
array2.filter(e=> array1.indexOf(e) < 0);
Plunker for you
_.difference gives you only the elements that are in the 1st array but not in the second one, nothing about the elements on the array 2 that are not in the array 1.
Is this what you want to achieve?

How to get all values in a specific property within a list of objects [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 7 years ago.
I have an array that looks like this:
var array = [{name:"AName1", value: 1},{name:"AName2", value: 2}, ...];
How do I get all the values from a specific property? Say, I want to get all the names from every object in the array, creating a new array with those names ["AName1, "AName2", ...]
I've tried to use _.pick from underscore.js:
var result = _.map(array, function (current) {
return _.pick(current, 'Name');
});
but it creates another array of objects with only the name property, which is not what i want to do
Any help is appreciated, thanks!
Using map like this:
array.map(function(item){ return item.name; });
The map() method creates a new array with the results of calling a
provided function on every element in this array.

Move object in array to end [duplicate]

This question already has answers here:
Move an array element from one array position to another
(44 answers)
Closed 8 years ago.
I'm trying to find a way to move an object to the end of the array
I have this array of objects:
[{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}]
Let's say I have an id: 3, or a position: 2.
With that I want to move the whole set {"id":"3","name":"Simon"} to the end of it all
I have tried so many things, and searched and searched but I can't make it work
You can splice and then concat the object you want to remove:
var array = [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}];
var itemToReplace = array.splice(0, 1); // 0 is the item index, 1 is the count of items you want to remove.
// => [{"id":"4","name":"Boaz"}]
array = array.concat(itemToReplace);
or even simpler:
array = array.concat(array.splice(0, 1));
BTW: it's an array of objects, not an object of arrays.
You can use splice and concat array methods like
var arr = [{"id":"4","name":"Boaz"},{"id":"2","name":"Shareen"},{"id":"3","name":"Simon"},{"id":"1","name":"Miriam"}];
// Consider need move arr[2] to the end
var removed = arr.splice(2,1);
var new_arr = arr.concat(removed);

Categories

Resources