Reputation: 429
I have the following data:
var data = [{
County: "Cork",
date: '1/1/2018',
num: 100
}, {
County: "Roscommon",
date: '07/10/2017',
num: 27
}];
var Counties = {};
for (County in data) {
for (var i = 0; i <= 1; i++) {
Counties[data[i].County] = data[i].County;
}
}
This returns {County: "Roscommon"}
How can I get my code to return every County in the array object so that I end up with:
{County: "Cork"}
{County: "Roscommon"}
Upvotes: 1
Views: 234
Reputation: 1148
You can use the array#map function.
const countyArray = data.map(el => {el.County})
or
const counties = [];
for (let county of data) {
counties.push({ County })
}
As an object:
const counties = {};
for (let element of data) {
counties[element.County] = {County: element.County};
}
Upvotes: 1
Reputation: 22524
You can use array#map
. You can iterate on your array using the array#map
and in its callback function, you can use object destructuring syntax to get the County
value and using the object short-hand notation create the object with County
value.
var data = [{ County: "Cork", date:'1/1/2018', num:100 },{ County: "Roscommon", date: '07/10/2017', num:27 }],
counties = data.map(({County}) => ({County}));
console.log(counties);
Upvotes: 4
Reputation: 33726
Your code has some issues, let's annalye them:
Your approach:
var Counties={};
for (County in data){
for (var i=0;i<=1;i++){
Counties["County"]=data[i].County;
}
}
The keyword in
isn't well used here, because that operator returns the index for every item in your Array data
for (County in data)
^
You don't need that inner for-loop
because you can iterate over data
array directly :-)
for (County in data){
for (var i=0;i<=1;i++){ // Remove that line
^
As you state in your question, your desired output is an array of objects. So you need to declare an Array.
var Counties={};
^
Look this code snippet, it's cleaner and follow your logic:
var data = [{
County: "Cork",
date: '1/1/2018',
num: 100
}, {
County: "Roscommon",
date: '07/10/2017',
num: 27
}];
var array = [];
for (c in data) {
array.push({"County": data[c].County});
}
console.log(array);
See?, your logic is working now!
A short approach using reduce
function will be:
The reduce
won't modify your original Object.
var data = [{
County: "Cork",
date: '1/1/2018',
num: 100
}, {
County: "Roscommon",
date: '07/10/2017',
num: 27
}];
var counties = data.reduce((a, c) => {
return [...a, ...[{"county": c.County}]]
}, []);
console.log(JSON.stringify(data));
console.log(JSON.stringify(counties));
See?, your original Obj wasn't modified and a new Array with the counties was created.
Another short approach using map
function will be:
The map
won't modify your original Object.
var data = [{
County: "Cork",
date: '1/1/2018',
num: 100
}, {
County: "Roscommon",
date: '07/10/2017',
num: 27
}];
var counties = data.map((c) => ({"county": c.County}));
console.log(JSON.stringify(data));
console.log(JSON.stringify(counties));
See?, your original Obj wasn't modified and a new Array with the counties was created.
Upvotes: 1
Reputation: 28722
Do you mean this? an array with objects with only the county values?
var data = [{
County: "Cork",
date:'1/1/2018',
num:100
},{
County: "Roscommon",
date: '07/10/2017',
num:27
}];
var Counties=data.map(function(a){return {County:a.County}});
console.log(Counties);
Or this, an array with just the county names?
var data = [{
County: "Cork",
date:'1/1/2018',
num:100
},{
County: "Roscommon",
date: '07/10/2017',
num:27
}];
var Counties=data.map(function(a){return a.County});
console.log(Counties);
For simple data mapping in an array I suggest you delve deeper in the functionality of map, which makes it pretty easy to "clean up" data in arrays to pick and choose the tidbits you want in the format you want it.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
If you need unique values you need a couple of extra steps
var data = [{
County: "Cork",
date:'1/1/2018',
num:100
},{
County: "Roscommon",
date: '07/10/2017',
num:27
},
{
County: "Roscommon",
date: '17/10/2017',
num:34
}
];
console.log("#nofilter");
var counties=data.map(function(a){
return a.County
});
console.log(counties);
console.log("as filtered array");
var counties=data.map(function(a){
return a.County;
})
.filter(function(value, index, self) {
return self.indexOf(value) === index;
});
console.log(counties);
console.log("as filtered objects");
var counties=data.map(function(a){return a.County})
.filter(function(value, index, self) {
return self.indexOf(value) === index;
})
.map(function(a) {
return {County: a}
});
console.log(counties);
Upvotes: 1
Reputation: 1
Probably the best option is to map over the data array like so. `
var data = [{
county: "Cork",
date:'1/1/2018',
num:100
},{
county: "Roscommon",
date: '07/10/2017',
num:27
}];
var counties=[];
data.map(item => {
var county = {county: item.county};
counties.push(county);
});
`
Upvotes: 0