This question already has answers here:
How to get a subset of a javascript object's properties
(36 answers)
One-liner to take some properties from object in ES 6
(12 answers)
Closed 4 years ago.
For example:
const obj = {name: 'jack', age: 18, gender: 'male'}
const {name, age} = obj
const obj2 = {name, age}
I want some key-value of a obj to make up a new obj, above is three line codes , I want to know if there is a way more concise? in one line?
Related
This question already has answers here:
Iterate through object properties
(31 answers)
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Let's say I have an object that has two key-values:
let object = {name: "abc", id: 12}
But let's say, I don't know one of the key-names, so:
let object = {<unknown_to_me>: "abc", id: 12}
How can I get the first key name if I know the other one? Their positions
THe first one is reachable via:
object.id
Can I get the other one by positions, the ! operator,...?
You could do so:
let object = {name: "abc", id: 12};
let knownKeyName = "id";
let objectKeys = Object.keys(object);
let unknownKeyName = objectKeys[objectKeys.length - 1 - objectKeys.indexOf(knownKeyName)];
console.log(unknownKeyName)
This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 1 year ago.
Hello i have an array of objects. Like this:
const obj = [{
count: 10,
value: count*2 // the previous count
}]
How can i reference 'count' on 'value' without having to find the index, or is a way to find the index of 'obj'?
You could take a getter with a reference to the same object.
const
objects = [{
count: 10,
get value () { return this.count * 2; }
}];
console.log(objects[0].value);
This question already has answers here:
Converting Json Object array Values to a single array
(5 answers)
Closed 2 years ago.
If I have an array such as:
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
Is it possible to make the array only containing the value pairs of the object such as:
let result = ["BSE","1010","STA","2020"];
Using Object.prototype.values, you can generate only values from an object.
let arr = [{subject: "BSE", courseCode: "1010"},{subject: "STA", courseCode: "2020"}];
const output = arr.flatMap((item) => Object.values(item));
console.log(output);
This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings into an array of objects
(6 answers)
Closed 3 years ago.
I want to convert an Array like:
[ 'John', 'Jane' ]
into an array of object pairs like this:
[{'name': 'John'}, {'name':'Jane'}]
Please help me to do so..
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);
This question already has answers here:
How can I add a key/value pair to a JavaScript object?
(26 answers)
Closed 6 years ago.
var John = { Cats: 2, Dogs: 3, Turtles: 1 };
var Mary = { Dogs: 0, Parakeets: 3};
How do I append new dimensions after I've already created the objects?
...John now also has 1 Parakeet
...Mary now also has 5 Koi
Just give them a value, like:
John.Parakeet = 1;
You can then later access these properties just like any other properties.
It really isn't hard.