Deleting an element from JS associative array [duplicate] - javascript

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
JavaScript Array Delete Elements
How is this done?
If I have the following array defined:
var myArr = [];
myArr[id1] = {prop1: prop1Value, prop2:prop2Value};
myArr[id2] = {prop1: prop1Value, prop2:prop2Value};
//etc
I wish to delete myArr[id1]

Perhaps you mean that the numerical indices after the deleted element aren't updated... in which case you'll need to use splice:
myArr.splice(id1, 1);

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);

Make a new array with one element of a multidimenional array using JavaScript [duplicate]

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

How to get values from a nested array in javascript? [duplicate]

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 4 years ago.
I have an array like let a = [[[1,2], [3,[1,2,3]]], [2,3]] and want to access the elements using a method/way to return the values like : 12312323 or [1,2,3,1,2,3,2,3]
how can I approach the solution in javascript/nodeJS?
thanks
You can use Array.flat()
let a = [[[1,2], [3,[1,2,3]]], [2,3]]
let op = a.flat(Infinity)
console.log(op)
You can check browser compatibility here compatibility

Delete random objects from array [duplicate]

This question already has answers here:
Picking 2 random elements from array
(10 answers)
Closed 7 years ago.
I have an array such as:
array=['a','b','c','d','e','f'];
I want to delete a random 2 elements. How can I do this?
To get two unique items from the array, and if you don't mind mutating the original array, you can use splice() to remove the selected item from the array so it won't be picked when you run it a second time:
var firstRandomChoice = array.splice(Math.floor(Math.random()*array.length), 1);
var secondRandomChoice = array.splice(Math.floor(Math.random()*array.length), 1);
If you use a utility library such as lodash, you may already have a function available to do this for you. For example, lodash provides sample(). So if you were using lodash, you could just do something like this to get an array of two random items:
var results = _.sample(array, 2);

Swap values of keys in JSON array [duplicate]

This question already has answers here:
Swap value of two properties on object(s)
(3 answers)
Closed 8 years ago.
I have the following JSON. I need to swap SortId
Like I have this,
[{"CategoryId":1,"Name":"Worktable","SortId":1}
,{"CategoryId":2,"Name":"Bf ","SortId":2}]
After swaping their 'SortId' I need
[{"CategoryId":1,"Name":"Worktable","SortId":2}
,{"CategoryId":2,"Name":"Bf ","SortId":1}]
Please tell me how to do it through JavaScript.
var tmp = a[0].SortId;
a[0].SortId = a[1].SortId;
a[1].SortId = tmp;
jsFiddle

Categories

Resources