Now, in my object collection,obj:
{date:2015/9/11,name:Ann,apple:1},
{date:2015/9/12,name:Ann,apple:0},
{date:2015/9/13,name:Ann,apple:1},
{date:2015/9/11,name:Bob,apple:1},.....
and I print it out like this:
2015/9/11 Ann 1
2015/9/12 Ann 0
2015/9/13 Ann 1
2015/9/11 Bob 1
2015/9/12 Bob 1
2015/9/13 Bob 1
Here is my code
var index, objLen
for (index = 0, objLen = obj.length; index<objLen; ++index){
console.log(obj[index].date +'\t'+ obj[index].name +'\t'+ result[index].apple)
}
I wish to output:
2015/9/13 Ann 2
2015/9/13 Bob 3
This shows how many apples Ann and Bob have eaten in this three days
I want to know how to get the output.
You can use a Array.prorotype.reduce to merge the items with the same name, storing the results in an object.
var obj = [
{ date: '2015/9/11', name: 'Ann', apple: 1 },
{ date: '2015/9/12', name: 'Ann', apple: 0 },
{ date: '2015/9/13', name: 'Ann', apple: 1 },
{ date: '2015/9/11', name: 'Bob', apple: 1 },
{ date: '2015/9/12', name: 'Bob', apple: 2 },
{ date: '2015/9/13', name: 'Bob', apple: 1 }
];
var merged = obj.reduce(function(merged, item) {
if (!merged.hasOwnProperty(item.name)) {
merged[item.name] = { date: item.date, name: item.name, apple: 0 };
}
merged[item.name].date = item.date;
merged[item.name].apple += item.apple;
return merged;
}, {});
// merged == {
// Ann: { date: '2015/9/13', name: 'Ann', apple: 2 },
// Bob: { date: '2015/9/13', name: 'Bob', apple: 4 }
// }
But this one results into an object with the person's name as the key. If you want to convert this back to an array of merged items, you can can make an array out of the values of the merged object using a combination of Object.keys and Array.prototype.map:
merged = Object.keys(merged).map(function(item) {
return merged[item];
});
// merged == [
// { date: '2015/9/13', name: 'Ann', apple: 2 },
// { date: '2015/9/13', name: 'Bob', apple: 4 }
// ]
You can then now use your code to print out the elements of this array.
Refer to the following links to read more about the different methods used in this solution:
Array.prototype.reduce
Array.prototype.map
Object.keys
Some functional approach to group and count element by passed key:
var obj = [{
date: '2015/9/11',
name: 'Ann',
apple: 1
}, {
date: '2015/9/12',
name: 'Ann',
apple: 0
}, {
date: '2015/9/13',
name: 'Ann',
apple: 1
}, {
date: '2015/9/11',
name: 'Bob',
apple: 1
}, {
date: '2015/9/12',
name: 'Bob',
apple: 2
}, {
date: '2015/9/13',
name: 'Bob',
apple: 1
}];
function groupBy(key, collenction) {
return collenction.reduce(function(group, element) {
if (!group[element[key]]) {
group[element[key]] = [element]
} else {
group[element[key]].push(element)
}
return group
}, {})
}
function countCollectionBy(key, collenction) {
return Object.keys(collenction).reduce(function(memo, element) {
memo[element] = countBy(key, collenction[element])
return memo;
}, {})
}
function countBy(key, collenction) {
return collenction.reduce(function(memo, element) {
return memo + element[key]
}, 0)
}
alert(JSON.stringify(countCollectionBy('apple', groupBy('name', obj))))
Related
I want to copy value of name and age into another array, below code is working fine, But I wanted to know better way to do it.
const users = [
{ id: 0, name: 'John', age:34 },
{ id: 1, name: 'Wayne', age:44 },
{ id: 2, name: 'David', age:24 },
];
let values=[];
users && users.map(user => {
values.push(user['name'])
values.push(user['age'])
})
console.log(values);
output
['John', 34, 'Wayne', 44, 'David', 24]
You can bind each items to an array containing both its name and age and then flattern these arrays.
This can be done using Array#FlatMap
const users = [
{ id: 0, name: 'John', age:34 },
{ id: 1, name: 'Wayne', age:44 },
{ id: 2, name: 'David', age:24 },
];
const nameAndAges = users.flatMap(user => [user.name, user.age])
console.log(nameAndAges)
Your solution looks good, this can be also solved in different ways, I guess you may want to create function for handling this.
const users = [
{ id: 0, name: 'John', age: 34 },
{ id: 1, name: 'Wayne', age: 44 },
{ id: 2, name: 'David', age: 24 },
];
const result = mapArrayToProps(users, ['name', 'age']);
function mapArrayToProps(arr, props) {
return arr.flatMap(obj => mapObjectToProps(obj, props));
}
function mapObjectToProps(obj, props) {
return props.map(prop => obj[prop])
}
console.log(result);
I see that all the loops for objects returns the key as string and the value, but I want to operate on the keys of the object itself. If I have this object:
const data = {
person1: [
{ id: 1, name: Mike, age: 24 },
{ id: 2, name: Bob, age: 31 }
],
person2: [
{ id: 3, name: Christin, age: 21 },
{ id: 4, name: Michelle, age: 33 }
],
}
const removePersonById = (id) => {
// Check which person the id belongs to and remove that person
const persons = Object.keys(data).map(person => ...)
}
I wanted to loop through data and run .includes on each person in order to remove them by the id, but I am at a loss on how to do that.
You can loop through all keys and delete that the person you want by id using the filter() method
const removePersonById = (id) => {
var all = Object.keys(data);
for(let person of all){
data[person] = data[person].filter(a => a.id!=id);
}
}
You could get the values and find the index. Then splice.
const
removePersonById = id => {
Object.values(data).forEach(a => {
const index = a.findIndex(o => o.id === id);
if (index !== 0) a.splice(index, 1);
});
};
You could use .some()
const data = {
person1: [
{ id: 1, name: "Mike", age: 24 },
{ id: 2, name: "Bob", age: 31 }
],
person2: [
{ id: 3, name: "Christin", age: 21 },
{ id: 4, name: "Michelle", age: 33 }
],
}
const removePersonById = (id) => {
// Check which person the id belongs to and remove that person
Object.keys(data).map(person => {
if (data[person].some(p => p.id === id)) delete data[person];
})
}
removePersonById(3)
console.log(data)
Use Object.entries() so you can iterate over the keys and values together. Then you can test the value to see if the id is found.
Then use the delete operator to remove that key from the object.
const removePersonById = id => {
delete data[id];
};
const data = {
person1: [
{ id: 1, name: "Mike", age: 24 },
{ id: 2, name: "Bob", age: 31 }
],
person2: [
{ id: 3, name: "Christin", age: 21 },
{ id: 4, name: "Michelle", age: 33 }
],
};
const removePersonById = (id) =>
Object.entries(data).forEach(([key, value]) => {
if (value.some(({id: personid}) => personid == id)) {
delete data[key];
}
});
removePersonById(3);
console.log(data);
let data = {
person1: [
{ id: 1, name: "Mike", age: 24 },
{ id: 2, name: "Bob", age: 31 }
],
person2: [
{ id: 3, name: "Christin", age: 21 },
{ id: 4, name: "Michelle", age: 33 }
],
}
const removePersonById = (id) => {
// Check which person the id belongs to and remove that person
data = Object.keys(data).reduce((acc,key) => {
if(data[key].some(person=>person.id===id)) return acc
acc[key]= data[key]
return acc
}, {})
console.log(`Remove person with ID ${id}: `,data)
}
removePersonById(1)
I'm just learning js now (late to the party I know) and I have the following code and I was wondering if it could be written in a cleaner/simpler way?
Also, ideally, instead of using "if (obj.id === 1)" I would like to iterate through the array and add age based on the sequence i.e. [0] would become '32' and so on.
const students = [ // Three objects, each with four properties
{
id: 1,
name: 'Mark',
profession: 'Developer',
skill: 'JavaScript'
},
{
id: 2,
name: 'Ariel',
profession: 'Developer',
skill: 'HTML'
},
{
id: 3,
name: 'Jason',
profession: 'Designer',
skill: 'CSS'
},
];
const studentsWithAge = students.map(obj => {
if (obj.id === 1) {
return {...obj, age: '32'};
} else if (obj.id === 2) {
return {...obj, age: '26'};
} else if (obj.id === 3) {
return {...obj, age: '28'};
}
return obj;
});
console.log(studentsWithAge);
// output
// [
// {
// id: 1,
// name: 'Mark',
// profession: 'Developer',
// skill: 'JavaScript',
// age: '32'
// },
// {
// id: 2,
// name: 'Ariel',
// profession: 'Developer',
// skill: 'HTML',
// age: '26'
// },
// {
// id: 3,
// name: 'Jason',
// profession: 'Designer',
// skill: 'CSS',
// age: '28'
// }
// ]
You can map the array into the object like so:
const ages = ['32', '26', '28'];
const studentsWithAge = students.map(obj => { ...obj, age: ages[obj.id-1] });
You could create an ages array and use the index to map the value to the corresponding object.
const students = [ // Three objects, each with four properties
{
id: 1,
name: 'Mark',
profession: 'Developer',
skill: 'JavaScript'
},
{
id: 2,
name: 'Ariel',
profession: 'Developer',
skill: 'HTML'
},
{
id: 3,
name: 'Jason',
profession: 'Designer',
skill: 'CSS'
},
];
const ages = [32, 26, 28];
const result = students.map((s, i) => {
return { ...s, age: ages[i] }
});
console.log(result);
Your code is true, another way to add ages by the id is the following code. Just use ids as object key and age as value.
The following code check if id exists in the ages const then add it to the studentsWithAge. It works exactly like your code.
const ages = {1: '32', 2: '26', 3: '28'};
const studentsWithAge = students.map(obj => {
if(ages[obj.id]) obj.age = ages[obj.id];
return obj;
});
But if you're sure all ids have age value simpler code like this could be used:
const ages = {1: '32', 2: '26', 3: '28'};
const studentsWithAge = students.map(obj => ({...obj, age: ages[obj.id]}));
The solution depends on how you store the ages data. Here's an example if you keep the ages data in an array of objects, just like you keep the students data.
This approach is easily extended, you can add any other fields related to the student to the object.
students = [
{id: 1,name: 'Mark',profession: 'Developer',skill: 'JavaScript'},
{id: 2,name: 'Ariel',profession: 'Developer',skill: 'HTML'},
{id: 3,name: 'Jason',profession: 'Designer',skill: 'CSS'}];
extraInfo = [{id: 1, age:32}, {id:2, age: 26}, {id:3, age: 33}];
const result = students.map((s)=>
({ ...s, ...extraInfo.find((a) => a.id === s.id) })
);
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
This question already has answers here:
Merge two array of objects based on a key
(23 answers)
Closed 1 year ago.
I have two arrays:
Array 1:
[
{
name: 'Bob',
traits: {
id: 1
}
}, {
name: 'Karl',
traits: {
id: 2
}
}, {
name: 'Joseph',
traits: {
id: 3
}
}
]
Array 2:
[
{
name: 'Karl',
user_id: 2,
dog: 'Rottweiler'
}, {
name: 'Joseph',
user_id: 3,
dog: 'Poodle'
}, {
name: 'Bob',
user_id: 1,
dog: 'Puppy'
}
]
Desired outcome:
I want to be able to merge the second array into the first array by finding what element user_id matches with id and then adding the object to array.
For example:
array 1 obj
{
name: 'Bob',
traits: {
id: 1
}
}
Since the id matches with array 2 obj user_id:
{
name: 'Bob',
user_id: 1,
dog: 'Puppy'
}
Final outcome will be:
{
name: 'Bob',
traits: {
name: 'Bob',
user_id: 1,
dog: 'Puppy'
}
}
arr2.forEach((obj) => {
const idx = arr1.findIndex((o) => o.traits.id === obj.user_id);
if (idx !== -1) {
arr1[idx] = { ...arr1[idx], traits: { ...obj } }
}
})
console.log(arr1[0]) // { name: 'Bob', traits: { name: 'Bob', user_id: 1, dog: 'Puppy' } }
Turn the second array into a map keyed by user_id, and then iterate the first array. Find the corresponding object in the map, and spread the matching object value into the traits property:
let arr1 = [{name: 'Bob',traits: {id: 1}},{name: 'Karl',traits: {id: 2}},{name: 'Joseph',traits: {id: 3}}];
let arr2 = [{name: 'Karl', user_id: 2,dog: 'Rottweiler'},{name: 'Joseph', user_id: 3,dog: 'Poodle'},{name: 'Bob',user_id: 1,dog: 'Puppy'}];
let map = new Map(arr2.map(item => [item.user_id, item]));
let result = arr1.map(item => {
let traits = map.get(item.traits.id);
return traits ? { ...item, traits} : item;
});
console.log(result);
As lookup in a map has an amortised time complexity of O(1), this is more efficient than finding the key in the array on every iteration (like with calling find).
You can easily achieve this result using map and find. Just map over the first array and find the element with obj.traits.id in the arr2. then return the desired result.
const arr1 = [
{
name: "Bob",
traits: {
id: 1,
},
},
{
name: "Karl",
traits: {
id: 2,
},
},
{
name: "Joseph",
traits: {
id: 3,
},
},
];
const arr2 = [
{
name: "Karl",
user_id: 2,
dog: "Rottweiler",
},
{
name: "Joseph",
user_id: 3,
dog: "Poodle",
},
{
name: "Bob",
user_id: 1,
dog: "Puppy",
},
];
const result = arr1.map((obj) => {
const { name, traits } = obj;
const isExist = arr2.find((o) => o.user_id === traits.id);
if (isExist) {
return { name, traits: { ...isExist } };
}
return obj;
});
console.log(result);
let a = [
{
name: 'Bob',
traits: {
id: 1
}
}, {
name: 'Karl',
traits: {
id: 2
}
}, {
name: 'Joseph',
traits: {
id: 3
}
}
];
let b = [
{
name: 'Karl',
user_id: 2,
dog: 'Rottweiler'
}, {
name: 'Joseph',
user_id: 3,
dog: 'Poodle'
}, {
name: 'Bob',
user_id: 1,
dog: 'Puppy'
}
];
a.map(aobj =>{
let sameIdObj = b.find( bobj => bobj.user_id === aobj.traits.id )
sameIdObj && (aobj.traits = sameIdObj)
})
console.log(a);
I want to make an array of objects grouped by the date property.
let data = [
{ Age: "(60-100)", Date: "28/05/20" },
{ Age: "(60-100)", Date: "28/05/20" },
{ Age: "(4-6)", Date: "28/05/20" },
{ Age: "(60-100)", Date: "29/05/20" },
{ Age: "(38-43)", Date: "29/05/20" },
{ Age: "(4-6)", Date: "29/05/20" },
{ Age: "(38-43)", Date: "30/05/20" },
{ Age: "(38-43)", Date: "30/05/20" }
];
I want the output like
result = [
{ Date: "28/05/20", "(60-100)": 2, "(4-6)": 1 },
{ Date: "29/05/20", "(38-43)": 1, "(4-6)": 1, "(60-100)": 1 },
{ Date: "30/05/20", "(38-43)": 2 },
]
Try this:
function groupByDate(data){
let groupedData = [];
data.forEach(element => {
//Search for the object containing the specified date
let objIndex = groupedData.findIndex(object => {return object.Date == element.Date;})
//If date is not created, create it
if (objIndex == -1){
groupedData.unshift({Date: element.Date})
objIndex = 0;
}
//If age is not created, create it. Else add 1 to specified age.
if(typeof groupedData[objIndex][element.Age] == 'undefined'){
groupedData[objIndex][element.Age] = 1;
} else {
groupedData[objIndex][element.Age]++;
}
});
return groupedData;
}
If you also want to sort by date, you could check out this post.
Hope it helped you!
Give this a try.
let data = [
{ Age: "(60-100)", Date: "28/05/20" },
{ Age: "(60-100)", Date: "28/05/20" },
{ Age: "(4-6)", Date: "28/05/20" },
{ Age: "(60-100)", Date: "29/05/20" },
{ Age: "(38-43)", Date: "29/05/20" },
{ Age: "(4-6)", Date: "29/05/20" },
{ Age: "(38-43)", Date: "30/05/20" },
{ Age: "(38-43)", Date: "30/05/20" }
];
let res = [];
data.map((d, index) => {
if (!res.some(val => val.Date === d.Date)) {
d[`${d.Age}`] = 1
res.push(d)
delete(d.Age)
} else {
let index = res.findIndex(val => val.Date == d.Date);
let _d = res[index];
if (_d.hasOwnProperty(`${d.Age}`)) {
_d[`${d.Age}`] = parseInt(_d[`${d.Age}`] + 1)
} else {
_d[`${d.Age}`] = 1
}
res[index] = _d;
}
})
console.log(res)
try this:
var result={};
for(var item of data) {
if(result[item.Date]==undefined) result[item.Date]={};
if(result[item.Date][item.Age]==undefined) result[item.Date][item.Age]=0;
result[item.Date][item.Age]++;
}
This gives you an object (not an array) with keys of Date and values as object with keys of Age and values as count.
If you still need an array, you can iterate over the result and construct an array.
result=={
"28/05/20": {
"(60-100)": 2,
"(4-6)": 1
},
"29/05/20": {
"(60-100)": 1,
"(38-43)": 1,
"(4-6)": 1
},
"30/05/20": {
"(38-43)": 2
}
}
If you want the array, you can create resultArr=[], iterate over keys of result, tempObj, add key "Date" and value of iterated key, then iterate over keys of iterated key, add each "Age" with it's count, then push tempObj into resultArr...
Condensed version based on #Dante Culaciati approach with optional sort parameter.
const condenseAge = (arr, isSort = true) => {
let r = [];
arr.map((val) => {
let i = r.findIndex(obj => obj.Date == val.Date);
(i < 0) && r.unshift({Date: val.Date}) && (i = 0);
(!r[i][val.Age]) ? r[i][val.Age] = 1 : r[i][val.Age]++;
});
return !isSort?r:r.sort((a,b)=>(ac=a['Date'].split('/'), bc=b['Date'].split('/'), new Date(ac[2],ac[1],ac[0]) - new Date(bc[2],bc[1],bc[0])));
}
console.log(condenseAge([
{ Age: "(4-6)", Date: "02/06/20"},
{ Age: "(60-100)", Date: "28/05/20" },
{ Age: "(60-100)", Date: "28/05/20" },
{ Age: "(4-6)", Date: "28/05/20" },
{ Age: "(60-100)", Date: "29/05/20" },
{ Age: "(38-43)", Date: "29/05/20" },
{ Age: "(4-6)", Date: "29/05/20" },
{ Age: "(38-43)", Date: "30/05/20" },
{ Age: "(38-43)", Date: "30/05/20" }
]));