How to remove item from a JavaScript object [duplicate] - javascript

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

Related

How to delete a element from a nested array in firestore [duplicate]

This question already has answers here:
Firestore Update single item in an array field
(2 answers)
how can i update a specific object property within an array in firebase [duplicate]
(2 answers)
Closed 4 months ago.
I want to delete the 0 element of questions array. I've tried this
const documentRef = db.collection(collection).doc(challengeId)
await documentRef.update({
[`stages.0.surveys.0.questions.0`]: firebase.firestore.FieldValue.delete()
})
But it doesn't work. I would appreciate any help. thank you

Show or hide class as a parameter [duplicate]

This question already has answers here:
How do I concatenate a string with a variable?
(5 answers)
Closed 3 years ago.
This is basic ,
I can show or hide a class with :
$('.blink').show();
but I would like to do the same with a parameter so
var a = "blink";
$(a).show();
will not work obviously and I couldn't find the keywords to look for this in google.
You need to append a dot when you use that variable so that the $ will consider it as a class selector:
var a = "blink";
$('.'+a).show();

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

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

values of html_options with javascript [duplicate]

This question already has answers here:
jQuery get values of checked checkboxes into array
(9 answers)
Closed 8 years ago.
Using javascript, how can I get the array of values selected in checkboxes used within a smarty html_options. Ive tried $('#selectbox').val() which returns null.
$("#trigger").click(function () {
var selected = $("input:checked");
var values = [];
selected.each(function (i,d) {
values.push(d.value);
});
console.log(values);
});
http://jsfiddle.net/698Ec/ this is a demo

Categories

Resources