This question already has answers here:
Push is overwriting previous data in array
(2 answers)
Closed 2 years ago.
I am using an angularjs foreach and want to find data objects to load into another objects array.
When doing this, it seems like I am adding the variable and all the added objects change to the last variable values.
var p = {};
angular.forEach(d, function(personnel, index){
if(wo.doc.job === personnel.job){
p["EmployeeID"] = personnel.EmployeeID;
p["Number"] = personnel.Number;
wo.doc.personnel.push(p);
console.log(personnel);
}
});
If this finds to 2 employees for a job, they are added and as i watch the wo.doc object after the second object is added the 2 added objects are the same as the last object.
Make a new object in the loop.
angular.forEach(d, function(personnel, index){
if(wo.doc.WorkOrderDetailID === personnel.PrimeWorkOrderNum){
var p = {EmployeeID: personnel.EmployeeID, Number: personnel.Number};
wo.doc.personnel.push(p);
}
});
Related
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);
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have a JSON object in a JavaScript function:
[{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}]
That is saved to an Multi-dimensional Array
Is there a better way that I can make a make a new Array with only the PMID element without looping and building it. I know that this question is close to a duplicate but I'm not interested in a merge or a concat. Currently I'm doing
var newArray = [];
for (var i = 0; i < this.pmidList.length; i++) {
newArray.push(this.pmidList[i]["PMID"]);
}
This question maybe a duplicate but if the average person can't find it based upon the title then it should not be considered a duplicate. The solution is the same but the question titles are much different.
You can use the Array.map function.
const input = [{"PMID":31206477,"MemberID":1287,"recID":6352},
{"PMID":31202264,"MemberID":1245,"recID":5974},
{"PMID":31201299,"MemberID":1184,"recID":3012},
{"PMID":31196160,"MemberID":1241,"recID":3833}];
const output = input.map(item => item.PMID);
Documentation
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
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.
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);