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)
Related
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
}
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed last year.
I need a small Help in JS? i want to access mail_address of this nested object, how will I do it
const data =
{ members:
[ { id : '7d8c03e88a8386f6453340c1db56'
, mail_address : 'trial.om'
, uniqsl : 'c6cce01'
} ] }
const data = {
members: [
{
id: '7d8c03e88a8386f6453340c1db56',
mail_address: 'trial.om',
uniqsl: 'c6cce01',
}
]
}
console.log(data.members[0].mail_address) // Prints "trail.om"
members is a array of objects, but it has one single element. You access this element at the index 0.
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)
}
This question already has answers here:
Why does changing an Array in JavaScript affect copies of the array?
(12 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 3 years ago.
I send a get request to the api wichh returns data under form of
{
"message": "<SUCCESS>",
"result": [
{
"i": 1,
},
{
"i": 2,
},
{
"i": 3,
} ]
}
Then in javascript (angular component.ts) I want to create an array ob objects but after pushing the objects in the array every array contains the data of the last object:
.subscribe(
data => {
type MyArrayType = Array<UserOpinion>;
let array: MyArrayType = [];
let cont: CustomInterface = {
i: ''
}
data.result.forEach(function(entry) {
cont.i = entry["i"];
array.push(cont);
console.log(cont);
//right value is shown
})
//console.log(array)
//array contains the connect number of objects but the objects are set on the same value: "i" = 3
Any ideas why this happens? Thank you
This should do it:
.subscribe(
data => {
type MyArrayType = Array<UserOpinion>;
let array: MyArrayType = [];
data.result.forEach(function(entry) {
array.push(entry);
});
}
);
You dont need cont just for copying the current object to another, you can use the current loop element directly.
This question already has answers here:
Appending the count to duplicates in a javascript string array
(3 answers)
Closed 3 years ago.
Input:-
Here i'm having array object in which natraj is repeating so i need to append some count like given example.
var aa= [
{name:'natraj'},
{name:'ajai'},
{name:'natraj'},
{name:'ajai'},
{name:'barath'},
{name:'ajai'},
{name:'barath'},
{name:'natraj'},
]
output:-
[
{name:'natraj'},
{name:'ajai'},
{name:'natraj_1'},
{name:'ajai_1'},
{name:'barath'},
{name:'ajai_2'},
{name:'barath_1'},
{name:'natraj_2'},
]
Keep a running count:
var aa = [{name:'natraj'},{name:'ajai'},{name:'natraj'},{name:'ajai'},{name:'barath'},{name:'ajai'},{name:'barath'},{name:'natraj'},]
let counts = {}
aa.forEach(o => {
if (counts[o.name]) {
o.name += `_${counts[o.name]++}`
} else {
counts[o.name] = 1
}
});
console.log(aa)