This question already has answers here:
Array destructuring in JavaScript
(5 answers)
Closed 3 years ago.
How to destruct a value from array?
const ?? = { text: ['some text'] };
const { text: [someText]} = { text: ['some text'] };
console.log(someText);
Related
This question already has answers here:
Array.push return pushed value?
(7 answers)
Closed 4 months ago.
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject(subject) {
this.subjects = this.subjects.push(subject); //what's wrong with this line
}
}
student1.addSubject('Math');
console.log(student1.subjects);
// logs out 1 instead of ['Math'], .push isn't functioning properly
const student1 = {
id: 1,
name: "Reed",
subjects: [],
addSubject: function(subject) {
this.subjects.push(subject);
}
}
student1.addSubject('Math');
console.log(student1.subjects);
Array.push() returns the new length of the array, not the array itself.
Unless you have a reason to capture this value, you don't need to assign it:
addSubject(subject) {
this.subjects.push(subject);
}
This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 years ago.
Right now I have an array that looks like this
const array = [
{
value: 'received',
title: 'Hjá Birgja',
},
{
value: 'pending',
title: 'Yfirstandandi',
},
{
value: 'processing',
title: 'Í vinnslu',
},
]
and I would like this to return true
if(array.includes('processing'){
// do something
}
not exactly what you wanted since i'm not sure if you only wanted to search the value key but here's a solution
if (array.find(i => i.value === 'processing')) {
// do something
}
This question already has answers here:
Console.log showing only the updated version of the object printed
(3 answers)
Setting a variable equal to another variable [duplicate]
(3 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I am new to Javascript, but have Java experiance.
const person = {
name: "bin",
walk() {
console.log(this);
}
};
person.walk();
const per1 = person;
per1.name = "cin";
per1.walk();
const animal = {
type: "2",
m2() {
console.log("type");
}
};
const walk1 = person.walk.bind(animal);
walk1();
person.walk();
I am failing to understand the output at line number 1 which prints a value of cin for name. When the code executes at person.walk(), the value of name has not changed. Why is the value being printed as cin?
{name: "bin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
{name: "cin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
{type: "2", m2: ƒ}type: "2"m2: ƒ m2()__proto__: Object
{name: "cin", walk: ƒ}name: "cin"walk: ƒ walk()__proto__: Object
This question already has answers here:
Destructuring deep properties
(4 answers)
Closed 5 years ago.
Say I have an object with a shape like so:
{
rows: [
{some: fields,
go: here}]
}
, and say that, in a particular case, I knew that the length of rows is 1. How could I extract {some: fields, go: here} through destructuring?
I have attempted: {rows: [stuff]}, and {rows: stuff} but in both cases console.log(stuff) prints [{some: fields, go: here}] How can I do this through destructuring?
{rows: [stuff]} works fine:
const obj = {
rows: [
{some: 'fields',
go: 'here'}]
};
const { rows: [stuff] } = obj;
console.log(stuff);
This question already has answers here:
How to store objects in HTML5 localStorage/sessionStorage
(24 answers)
Closed 8 years ago.
I have the following:
var allItems = {Items: [
{Value: 1, Name: "String1"},
{Value: 2, Name: "String2"},
{Value: 3, Name: "String3"}
]
};
localStorage.setItem('allItems', allItems);
I'm struggling to access any properties of allItems after retrieving it from localStorage. How would I say access the Name of the second object in the array Items?
You have to use JSON stringify to store the object and then use parse to convert it back into a JSON object.
localStorage.setItem('allItems', JSON.stringify(allItems));
var storageItems = JSON.parse(localStorage.getItem('allItems'));
console.log(storageItems.Items[1].Name);