javascript-objects

how to map more than one property from array of object in javascript

拟墨画扇 提交于 2020-04-02 07:50:34
问题 I have an array of Object as follows: var obj = [ {a: 1, b: 5, c: 9}, {a: 2, b: 6, c: 10}, {a: 3, b: 7, c: 11}, {a: 4, b: 8, c: 12} ]; I know about how to get single object using Array.map() like this. var result = obj.map(x=>x.a) This will give me following result [1, 2, 3, 4] But I want result like follows: [ {a: 1, b: 5}, {a: 2, b: 6}, {a: 3, b: 7}, {a: 4, b: 8} ] inshort from array of object i want to select only few fields(more than one) how to do that ? 回答1: You can use .map() with

how to map more than one property from array of object in javascript

时光毁灭记忆、已成空白 提交于 2020-04-02 07:49:07
问题 I have an array of Object as follows: var obj = [ {a: 1, b: 5, c: 9}, {a: 2, b: 6, c: 10}, {a: 3, b: 7, c: 11}, {a: 4, b: 8, c: 12} ]; I know about how to get single object using Array.map() like this. var result = obj.map(x=>x.a) This will give me following result [1, 2, 3, 4] But I want result like follows: [ {a: 1, b: 5}, {a: 2, b: 6}, {a: 3, b: 7}, {a: 4, b: 8} ] inshort from array of object i want to select only few fields(more than one) how to do that ? 回答1: You can use .map() with

how to map more than one property from array of object in javascript

元气小坏坏 提交于 2020-04-02 07:47:28
问题 I have an array of Object as follows: var obj = [ {a: 1, b: 5, c: 9}, {a: 2, b: 6, c: 10}, {a: 3, b: 7, c: 11}, {a: 4, b: 8, c: 12} ]; I know about how to get single object using Array.map() like this. var result = obj.map(x=>x.a) This will give me following result [1, 2, 3, 4] But I want result like follows: [ {a: 1, b: 5}, {a: 2, b: 6}, {a: 3, b: 7}, {a: 4, b: 8} ] inshort from array of object i want to select only few fields(more than one) how to do that ? 回答1: You can use .map() with

Find index of object in javascript using its property name

人走茶凉 提交于 2020-03-26 06:38:33
问题 I want to find Index of javascript array of objects using objects property name. My code is :- const checkbox = [{'mumbai': true},{'bangalore': true},{'chennai': true},{'kolkata': true}]; How can i find index of chennai? Can i acheive using lodash? 回答1: You can use .findIndex() const checkbox = [ {'mumbai': true}, {'bangalore': true}, {'chennai': true}, {'kolkata': true} ]; const finder = (arr, key) => arr.findIndex(o => key in o); console.log(finder(checkbox, 'chennai')); console.log(finder

typescript: How to get objects with the same property values but different keys from 2 different sets of Objects

别说谁变了你拦得住时间么 提交于 2020-03-25 18:39:57
问题 I have to sets of Json got from the form the state data objetSet1: {id: 12, name: 'Foo Bar', email: 'foo@bar.com'}, {id: 23, name: 'Bar Foo', email: 'bar@foo.com'}, {id: 61, name: 'Barbell', email: 'barbell@mail.com'}, {id: 45, name: 'Joe Ocean', email: 'joe@ocean.com'} objectSet2: {ObjectId:15, name: 'someone', email: 'someone@mail.com'}, {ObjectId: 23, name: 'sometwo', email: 'sometwo@mail.com'}, {ObjectId: 72, name: 'seven ', email: 'seven@mail.com'}, {ObjectId: 23, name: 'five ', email:

Copy array of Objects in angular 2

旧街凉风 提交于 2020-03-22 07:08:25
问题 I have two array called 'persons' and 'persons2', The 'persons2' array would to be copy of 'persons' array, But the problem is when I copy it, and I want to change the second array, the first array is also changing. This is my code: export class AppComponent { persons = [ { name:'David', lname:'Jeu' } ]; persons2=[...this.persons]; constructor(){ console.log(this.persons[0]); this.persons2[0].name='Drake'; console.log(this.persons[0]); console.log(this.persons2[0]); } } 回答1: But the problem

Copy array of Objects in angular 2

自古美人都是妖i 提交于 2020-03-22 07:07:28
问题 I have two array called 'persons' and 'persons2', The 'persons2' array would to be copy of 'persons' array, But the problem is when I copy it, and I want to change the second array, the first array is also changing. This is my code: export class AppComponent { persons = [ { name:'David', lname:'Jeu' } ]; persons2=[...this.persons]; constructor(){ console.log(this.persons[0]); this.persons2[0].name='Drake'; console.log(this.persons[0]); console.log(this.persons2[0]); } } 回答1: But the problem

Add property to object when it's not null

不问归期 提交于 2020-03-13 06:06:09
问题 I'm working on a small API and I want to update the data using HTTP PATCH REQUEST without using a bunch of if statements. I'm trying to fill the outgoing data object with the changed data only. update() { let prop1 = hasBeenChanged.prop1 ? changedData.prop1 : null; // ... let propN = hasBeenChanged.propN ? changedData.propN : null; let data: ISomething = { // something like --> property != null ? property: property.value : nothing } } Is there any way to create the data object dynamically?

Add property to object when it's not null

谁说胖子不能爱 提交于 2020-03-13 06:04:55
问题 I'm working on a small API and I want to update the data using HTTP PATCH REQUEST without using a bunch of if statements. I'm trying to fill the outgoing data object with the changed data only. update() { let prop1 = hasBeenChanged.prop1 ? changedData.prop1 : null; // ... let propN = hasBeenChanged.propN ? changedData.propN : null; let data: ISomething = { // something like --> property != null ? property: property.value : nothing } } Is there any way to create the data object dynamically?

Add property to object when it's not null

岁酱吖の 提交于 2020-03-13 06:04:29
问题 I'm working on a small API and I want to update the data using HTTP PATCH REQUEST without using a bunch of if statements. I'm trying to fill the outgoing data object with the changed data only. update() { let prop1 = hasBeenChanged.prop1 ? changedData.prop1 : null; // ... let propN = hasBeenChanged.propN ? changedData.propN : null; let data: ISomething = { // something like --> property != null ? property: property.value : nothing } } Is there any way to create the data object dynamically?