How might I extract the property values of a JavaScript object into an array?
var dataArray = [];
for(var o in dataObject) {
dataArray.push(dataObject[o]);
}
From an array of objects, extract value of a property as array
Here is a shorter way of achieving it:
let result = objArray.map(a => a.foo);
OR
let result = objArray.map(({ foo }) => foo)
You can also check Array.prototype.map()
.
Extract values from javascript object
What you need is an array. In this case you have something that should be an array of member names. To get the count you can just get the length of the array with .length. To assign the contents of the array to individual variables you can use array destructuring.
Here's what the code would look like:
const memberNames = [
"Jake Bayliss",
"Daryl Tripp",
"Tom McKenzie",
"George Bell",
"Ally Young"
];
const [member1, member2, member3, member4, member5] = memberNames
const count = memberNames.length
This will create an object called members with an arbitrary number of properties, named member1, member2, member3 etc.
const members = memberNames.reduce((previous, memberName, index)=>({
...previous,
['member'+(index+1)]: memberName
}),{})
Result:
{
member1: 'Jake Bayliss',
member2: 'Daryl Tripp',
member3: 'Tom McKenzie',
member4: 'George Bell',
member5: 'Ally Young'
}
You'll be able to use the dot operator on members to get each value. E.g. members.member1
From an array of objects, extract value of properties for each object and put in a different array
According to your desired result, I think you can use ES6 functions.
const result = yourTable.map(element => Object.values(element));
Using map()
function, you go through all elements, and extract from each object its values.
Extract value from array of objects
One more variant:
var arr = [{ type: 'month', value: '9' },
{ type: 'day', value: '11' },
{ type: 'year', value: '2021' },
{ type: 'hour', value: '7' },
{ type: 'minute', value: '35' },
{ type: 'second', value: '07' }];
// var month = arr.filter(x => x.type == 'month')[0].value; // less efficient
var month = arr.find(x => x.type == 'month').value; // more efficient
console.log(month) // 9
How to get all properties values of a JavaScript Object (without knowing the keys)?
By using a simple for..in
loop:
for(var key in objects) {
var value = objects[key];
}
Extract a key/value object type from the properties of an array of objects in Typescript
The following should do the trick:
const allData = [
{
name: "info",
content: { hey: "you" },
},
{
name: "other",
content: { bro: "chill" },
},
] as const;
type ContentInstances = {
[K in typeof allData[number]["name"]]: Extract<
typeof allData[number],
{ name: K }
>["content"];
};
declare const foo: ContentInstances;
foo.info.hey;
foo.other.bro;
The type ContentInstances
iterates through all the "name"
keys of the objects contained into allData
. It maps each key into the union of the objects having that key, to then indexing the union with the "content"
key.
From an array of objects, extract value of two properties into an array
This should work - if your elements don't have a foo
or anotherfoo
they'll come out as undefined
objArray = [{ foo: 1, bar: 2, anotherfoo: 5}, { foo: 3, bar: 4, anotherfoo: 8}];
function myfunction(arr) { return arr.map(function(e) { return { foo: e.foo, anotherfoo: e.anotherfoo }; });}
newArray = myfunction(objArray);console.log(newArray);
// newArray is [{foo:1, anotherfoo:5},{foo:3, anotherfoo:8}]
Related Topics
Why Does the Promise Constructor Need an Executor
How to Enable Touch on Multiple Buttons Simultaneously in React Native
Multiple, Sequential Fetch() Promise
Applying Classname/Onclick/Onchange Not Working on Custom React Component
How to Change the HTML Content as It's Loading on the Page
How to Save HTML Page as PDF Using JavaScript or Jquery
What's the Difference Between Returning Value or Promise.Resolve from Then()
Resize Svg When Window Is Resized in D3.Js
Embed a JavaScript Engine in an iOS Application
Jquery UI - Close Dialog When Clicked Outside
React - Changing an Uncontrolled Input
Why Is "This" in an Anonymous Function Undefined When Using Strict
How to Split a Long Regular Expression into Multiple Lines in JavaScript
Wkwebview - Complex Communication Between JavaScript & Native Code
How to Use Protractor on Non Angularjs Website
Unobtrusive JavaScript: <Script> at the Top or the Bottom of the HTML Code
What's the Best Way to Calculate Date Difference in JavaScript