Nested destructuring [duplicate] - javascript

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

Related

What's wrong with this method? the .push() method isn't working as expected [duplicate]

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

How to attach array into the object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
I have an object where i am associating nested objects. In few places I want to associate array instead of object. Any help how to achive that.
Ex :
{ "Id": "ID1",{
"Data1" : {
"myData1" : "myVal1"
},
"Data2":[{
"Dat1" : "val1"
},{
"Dat2" : "Val2"
}]
}
}
So in this case somewhere I am attacahing associate by using this code
`ID1[Data1] = "Data1" : {myData1" : "myVal1"}`
Can any body please suggest me how to add array.
You can achieve it as follows:
const obj = {};
obj['prop'] = {
test: 'test'
};
obj['arr'] = [];
obj['arr'].push({
test: 'test'
});
console.log(obj);

How to delete the element of an object if the element is array? [duplicate]

This question already has answers here:
Remove property for all objects in array
(18 answers)
Closed 3 years ago.
want to remove some fields from a complex object.
var obj1={title:"T1",name="name1",classes:[
{id:1,scheme:1,cName:"Cls1"},
{id:2,scheme:2,cName:"Cls2"},
{id:3,scheme:3,cName:"Cls3"},]}
I want to remove 'scheme' & 'cName' from classes for every class
You can use map() on obj.classes and destrcuture the properties to be removed and return the other properties.
Note: The below method will modify original object.
var obj1={title:"T1",name:"name1",classes:[ {id:1,scheme:1,cName:"Cls1"}, {id:2,scheme:2,cName:"Cls2"}, {id:3,scheme:3,cName:"Cls3"}]}
obj1.classes = obj1.classes.map(({cName,scheme,...rest}) => rest);
console.log(obj1);
If there are only three properties scheme, id and cName and you want to remove two of them. Then its better to return the remain property from map()
var obj1={title:"T1",name:"name1",classes:[ {id:1,scheme:1,cName:"Cls1"}, {id:2,scheme:2,cName:"Cls2"}, {id:3,scheme:3,cName:"Cls3"}]}
obj1.classes = obj1.classes.map(({id}) => ({id}));
console.log(obj1);
Use map() with your classes property.
map() will return new array according to the property you want to get.
If you want to an id collection, just do the following code:
var obj1={
title:"T1",
name: "name1",
classes: [
{id: 1, scheme: 1, cName: "Cls1"},
{id:2, scheme: 2, cName: "Cls2"},
{id:3, scheme: 3, cName: "Cls3"}
]
}
obj1.classes = obj1.classes.map((item) => item.id);
console.log(obj1)
You can do it like,
let obj1={title:"T1",
name:"name1",
classes:[{id:1,scheme:1,cName:"Cls1"},
{id:2,scheme:2,cName:"Cls2"},
{id:3,scheme:3,cName:"Cls3"}]};
obj1.classes.forEach((el) => { delete el.scheme;
delete el.cName;});
console.log(obj1);

JS Recursive object assign [duplicate]

This question already has answers here:
How to deep merge instead of shallow merge?
(47 answers)
Closed 4 years ago.
I learned that when using Object.assign() it extends only the top level object. How can I deeply extend the object? For example, let's say I have the following source object:
const source = {
id: 1,
otherKey: {},
params: {
page: {
a: 1,
b: {}
},
data: {
b: 1
}
}
}
And I am using Object.assign() like this:
Object.assign({}, source, {
params: {
page: {
a: 2
}
}
}
The result will be:
{
id: 1,
otherKey: {},
params: {
page: {
a: 2
}
}
}
How can I preserve the params.data key and the params.page.b key in a shallow clone way.
oldObject.params.data === newObject.params.data // true
oldObject.params.page === newObject.params.page // false
oldObject.params.page.b === newObject.params.page.b // true
Note: This question is not the same as How to deep merge instead of shallow merge. The answers there does not give the expected results.
Check this bin that takes an answer from the above link.
So in your posted code, what happens is, if the source and target both contain the same key then nothing happens. The object is recursed down to the children. However if you simply change the inner block to this:
if (!target[key]) {
Object.assign(target, { [key]: {} });
}else{
target[key] = Object.assign({}, target[key])
}
mergeDeep(target[key], source[key]);
This will create a new assigned object for any key that is found in both the source and the target. Interestingly though, if you do this, your expected falseys will not show up in the console. This is because the target will always match the result, as it is the final mutated object. So in order to get your expected false results, you need to do something like the following:
var tester = source.params
const result = mergeDeep(source, b)
console.log(tester === result.params) // this will be false after the above addition
You can see your desired result here: http://jsbin.com/vicemiqiqu/1/edit?js,console

Accessing some data of an object inside an array of objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
Been looking around haven't find a solution.. Might be a really stupid question (probably) but haven't found a way to access to it.
I have the class Iphone:
export class Iphone{
version: string;
fixes = [
{fixlcdprice:null},
{fixspeakerprice:null}
];
}
then I have an array of Iphone with data
export const IPHONES:Iphone[]=[
{
version:'Iphone 4',
fixes:[
{fixlcdprice:19},
{fixspeakerprice:19}
]
},
{
version:'Iphone 4s',
fixes: [
{fixlcdprice:19},
{fixspeakerprice:29}
]
}
]
trying to access the price of the fixes but I can't.
have tryed
Iphone.fixes[0] <-- returns (object, object)
then tryed
Iphone.fixes[0[0]] <-- returns nothing..
Iphone.fixes.fixlcdprice <-- doesnt work
Looks like you want
Iphone.fixes[0].fixlcdprice
Iphone=[
{
version:'Iphone 4',
fixes:[
{fixlcdprice:19},
{fixspeakerprice:19}
]
},
{
version:'Iphone 4s',
fixes: [
{fixlcdprice:19},
{fixspeakerprice:29}
]
}
]
Iphone[0].fixes[0] gives you the disired result

Categories

Resources