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.
Related
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:
Javascript string array to object [duplicate]
(4 answers)
Closed 3 years ago.
I would like to convert a javascript array which looks like:
['https://www.google.com', 'https://www.facebook.com']
to a list of JSON objects that looks like this:
[{"redirectUri": "https://www.google.com"},
{"redirectUri": "https://www.facebook.com"}]
I have tried using Object.assign({}, array);
however this retuns json with the parameter name as the index of the array value and are all in a single object:
{"0": "https://www.google.com", "1": "https://www.facebook.com"},
is there a way to change this to use a custom parameter name dynamically?
You just need to map your elements respectively, using Array.map() method:
let result = arr.map(o => {
return {
"redirectUri": o
}
});
Demo:
let arr = ['https://www.google.com', 'https://www.facebook.com'];
let result = arr.map(o => {
return {
"redirectUri": o
}
});
console.log(result);
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)
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)
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);