Swap values of keys in JSON array [duplicate] - javascript

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

Related

How to get the one before last element of an array in JavaScript? [duplicate]

This question already has answers here:
get the second to last item of an array?
(11 answers)
Closed 10 months ago.
I wanna to get one before last element of array in JavaScript. but in console, when I use this code "array.length -2"dose not work, and it just return me the length of array minus 2.
and when I use this code "array.at[-2]" it returns "undefined"!
const arr = [1,2,3,4,5,6]
console.log(arr[arr.length -2])
//5

Text Array to Array in Javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 3 years ago.
I need convert from data = "[[0,1], [1,1]]" to data = [[0,1], [1,1]] in javascript, remove double quotes . It's possible?
Thanks
Just use the JSON object:
data = JSON.parse("[[0,1], [1,1]]"); // data === [[0,1], [1,1]]

Add double quotes to arraylist of string in javascript [duplicate]

This question already has answers here:
How to split comma separated string using JavaScript? [duplicate]
(4 answers)
Closed 3 years ago.
I am new to stack overflow learning javascript and programming.
I have a problem that i am stuck while learning and thinking any help on this question will be
useful for me thanks and the question is:
Example i have a variable a in the code below and i want to convert it to an array in javascript
var a = ["baby,cat,dog"]
i wanted it to be
a = ["baby","cat","dog"].
Use String.prototype.split() as below
var a = ["baby,cat,dog"];
a = a[0].split(',');
console.log(a);
You could take the array and map the splitted values and get a flat array back.
var array = ["baby,cat,dog"];
result = array.flatMap(s => s.split(','));
console.log(result);

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

How to remove item from a JavaScript object [duplicate]

This question already has answers here:
How do I remove a property from a JavaScript object?
(37 answers)
Closed 3 years ago.
How can I remove an item from a JavaScript object?
Like this:
var test = {'red':'#FF0000', 'blue':'#0000FF'};
test.remove('blue');
var test = {'red':'#FF0000', 'blue':'#0000FF'};
delete test.blue; // or use => delete test['blue'];
console.log(test);
this deletes test.blue

Categories

Resources