How to get data from object in array [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 25 days ago.
<td>
{{tickets.forEach((element) => element.ticketCategories.price))}}
</td>
tickets = [
{
ticketCategories: {
type: 'Presale 1 Ticket',
price: 75000,
},
sumTicket: 3,
},
];
let result = tickets.map(({ticketCategories}) => ticketCategories.price)
console.log(result); // output: [75000]
how to get 75000 not [75000]
let result = tickets.map(({ticketCategories}) => ticketCategories.price)
console.log(ressult); // output: [75000]
i try use map but i get [75000] not 75000

The map function returns an array. you can use forEach to log out each element of the result array or use join to join all the result elements to one string.
result.forEach(res => console.log(res))
console.log(result.join(", "))

Related

How to access age value from the array of object in javascript? [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 months ago.
const data = [{
"hello":'thameem',
"age":24
},
{
"hello":'thameem',
"age":25
}];
console.log(data);
I need all age values
let data = [{ "hello":'thameem', "age":24 }, { "hello":'thameem', "age":25 }];
// will give you an array to ages
data = data?.map(item => item.age)
Use map to create an array
const ages = data.map((d) => d.age);
console.log(ages);
If you don't need new array and just want to access the property, you may use forEach
data.forEach((d) => {
// do something with your data
}

How to Get Only the Text inside the Brackets [{}]? [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
const data = { user: [ { name: 'jack' } ] }
How do I get only Jack when I call the constant.
It is saved as an array.
To use its components, you must do the following:
data.user[0].name;
data.user[1].name;
data.user[1].age;
If you want foreach loop in JS you can do like this :
#EDIT 1 updated without this
const data = [{name:"Jack", age:"50"},{name:"Brook", age:"20"},{name:"Noob", age:"34"}];
for(const n of data) {
console.log(n.name)
console.log(n.age)
}
#EDIT2
const data = {user : [{name:"Jack", age:"50"},{name:"Brook", age:"20"},{name:"Noob", age:"34"}]};
for(const n of data.user) {
console.log(n.name)
console.log(n.age)
}

extract sub parameters from array with objects [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I have next array:
let someArr = [
{param:{name: 'bla'}},
{param:{name: 'blu'}}
];
how I can get array without 'param'?
[{name: 'bla'},{name: 'blu'}]
let someArr = [
{param:{name: 'bla'}},
{param:{name: 'blu'}}
];
const result = someArr.map(el => el.param);
console.log(result);
let newArray = someArr.map(item => {
return item.param;
});

Create JavaScript Array from Object Keys [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
I have the following JavaScript Array:
supportedCurrencies = [
{
"currencyCode":"USD",
},
{
"currencyCode":"CAD",
},
{
"currencyCode":"GBP",
}
]
My goal is to get the currencyCode values and create an array (ex. [USD, CAD, GBP...] and then join these arrays with | ...to get final string output USD|CAD|GBP
supportedCurrencies.map(key => {
// Map through JS object
// Get currencyCode values
// Create new array with values
// Join these values with |
})
supportedCurrencies = [
{
"currencyCode":"USD",
},
{
"currencyCode":"CAD",
},
{
"currencyCode":"GBP",
}
]
const info = supportedCurrencies.map(el => el.currencyCode).join("|")
console.log(info)

In javascript, how to convert string array into array of object? [duplicate]

This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 4 years ago.
I have the following array of string that needs to be turn into array of object with the keys value and label
languages: ["Arabic", "Irish"]
0:"Arabic"
1:"Irish"
length: 2
If I wanted to make this array into an array of object like the following:
languages = [
{ value: 'Arabic', label: 'Arabic' }
{ value: 'Irish', label: 'Irish' }
]
How will I do that? Can someone please help?
You can use Array.map():
var languages = ["Arabic", "Irish"];
var res = languages.map(item => ({'value':item, 'label':item}));
console.log(res);
You can also use Array.forEach() as:
var languages = ["Arabic", "Irish"];
var res = [];
languages.forEach(item => res.push({'value':item, 'label':item}));
console.log(res);

Categories