How to merge Array of Objects based on the same value? - javascript

I have this array of Objects:
var array = [{
country: "Austria",
name: "2019-01-04T23:00:00.000Z",
value: "1"
},
{
country: "Austria",
name: "2019-01-11T23:00:00.000Z",
value: "3"
},
{
country: "Austria",
name: "2019-01-18T23:00:00.000Z",
value: "1"
}
]
I want manipulate this to achieve this result:
var array = [{
country: "Austria",
series: [{
name: "2019-01-04T23:00:00.000Z",
value: "1"
},
{
name: "2019-01-11T23:00:00.000Z",
value: "3"
},
{
name: "2019-01-18T23:00:00.000Z",
value: "1"
}
]
}]
I read many questions but none helped me.

You could loop thorugh the array. Use destructuring to get country and rest of the properties separately. Add each unique country to group object as key and push the rest object to the series array. Then use Object.values() to get the values as an array
const array=[{country:"Austria",name:"2019-01-04T23:00:00.000Z",value:"1"},{country:"Austria",name:"2019-01-11T23:00:00.000Z",value:"3"},{country:"Austria",name:"2019-01-18T23:00:00.000Z",value:"1"}];
const group = {};
array.forEach(({ country, ...rest }) => {
group[country] = group[country] || { country, series: [] };
group[country].series.push(rest)
})
console.log(Object.values(group))

This should do:
var map = {};
for(var entity of array) {
if(!map[entity.country]) {
map[entity.country] = {
country: entity.country,
series: [
{
name: entity.name,
value: entity.value
}
]
};
}
else {
map[entity.country].series.push({
name: entity.name,
value: entity.value
});
}
}
var mappedArray = Object.values(map);

Here is functional solution without for loops and mutable variables:
const result = array.reduce((carry, item) => {
if (!carry.includes(item.country)) {
carry.push(item.country);
}
return carry;
}, []).map(country => {
return {
country: country,
series: array.filter(item => item.country === country).map(item => {
return {
name: item.name,
value: item.value
};
})
};

You can do something like:
const result = array
.map(
c => ({
country: c.country,
series: array
.filter(d => d.country === c.country)
.map(
d => ({
name: d.name,
value: d.value
})
)
})
)

Related

How to convert array of objects into enum like key value pair in javascript?

I have an array
const a = [
{ name: "read-web-courses" },
{ name: "example" },
{ name: "t_gql" },
{ name: "ddddd" },
];
I am trying it to reduce it to the below given output , However I am stuck
Output
{0:"read-web-courses",1:"example",2:"t_gql",3:"ddddd"}
You could map the wanted property and assign the pairs to the object.
const
array = [{ name: "read-web-courses" }, { name: "example" }, { name: "t_gql" }, { name: "ddddd" }],
result = Object.assign({}, array.map(({ name }) => name));
console.log(result);
You can use Array.reduce like below.
const a = [
{ name: "read-web-courses" },
{ name: "example" },
{ name: "t_gql" },
{ name: "ddddd" },
];
const convert = arr => (
arr.reduce((total, value, index) => {
total[index] = value.name;
return total;
}, {})
)
console.log(convert(a));
This is accomplished using Array#reduce, where you can use the index from the reduce callback as the key of the new object:
const a = [ { name: "read-web-courses" }, { name: "example" }, { name: "t_gql" }, { name: "ddddd" }];
const res = a.reduce((r, o, i) => {
r[i] = o.name;
return r;
}, {});
console.log(res);
Also one more approach using Object#fromEntries and Array#map, where each object is converted to an array of key, value pairs:
const a = [ { name: "read-web-courses" }, { name: "example" }, { name: "t_gql" }, { name: "ddddd" }];
const res = Object.fromEntries(a.map((o, i) => [i, o.name]));
console.log(res)

Map being skipped on array of objects

I can't figure out why my map is not resolving. It's somehow skipping my map even though countriesAtoH shows me that it does have an array of objects in it but when I try to map over it, it's like it doesn't map it, because I can't put breakpoints inside map and map ultimately is not invoked. Meaning I get a length of 0 on options
const countries = {
Afghanistan: {
countryId: 0,
name: "Afghanistan"
},
Albania: {
countryId: 1,
name: "USA"
},
Algeria: {
countryId: 99,
name: "Canada"
}
}
export const countryOptions = () => {
let countriesAtoH = [];
Object.keys(countries).forEach((c) => {
if (countries[c].countryId <= 97) {
countriesAtoH[c] = countries[c];
}
});
const options = countriesAtoH.map(country => {
const c = {
text: {
type: "plain_text",
text: country.name,
},
value: country.countryId
}
return c;
});
return options;
}
In this line:
countriesAtoH[c] = countries[c];
c is country name as string, not number as array index, so the country will not be push to the array. So the array is still empty as Array(0) in your log.
You may want to do array push here:
countriesAtoH.push(countries[c]);
You have problems on countriesAtoH[c] = countries[c] part.
The type of countriesAtoH is array so the index should be the number and you are assigning c value (string) value as the index on countriesAtoH array.
Therefore, it is not working. It's better to set countriesAtoH = {} as object and use Object.values(countriesAtoH) on getting options variable.
const countries = {
Afghanistan: {
countryId: 0,
name: "Afghanistan"
},
Albania: {
countryId: 1,
name: "USA"
},
Algeria: {
countryId: 99,
name: "Canada"
}
}
const countryOptions = () => {
let countriesAtoH = {};
Object.keys(countries).forEach((c) => {
if (countries[c].countryId <= 97) {
countriesAtoH[c] = countries[c];
}
});
const options = Object.values(countriesAtoH).map((country) => {
const c = {
text: {
type: "plain_text",
text: country.name,
},
value: country.countryId
}
return c;
});
return options;
}
console.log(countryOptions());
Simply, you can use Array.prototype.filter and Array.prototype.map to get the options.
const countries = {
Afghanistan: {
countryId: 0,
name: "Afghanistan"
},
Albania: {
countryId: 1,
name: "USA"
},
Algeria: {
countryId: 99,
name: "Canada"
}
}
const countryOptions = () => {
const options = Object.values(countries).filter(({ countryId }) => countryId <= 97).map((item) => ({
text: {
type: "plain_text",
text: item.name
},
value: item.countryId
}));
return options;
}
console.log(countryOptions());

Algorithm from folderstring to correct folderstructure in javascript

I have an array of data which is a string of folders:
var data = [{ name: "/X" }, { name: "/X/Y" }, { name: "/X2" }, { name: "/X2/Z" }, { name: "/X/k" }]
For a component to display this items I need them sorted nested like these:
var data = [{ name: "/X", sub: [{ name: "/Y" }, { name: "/k" }]}, { name: "/X2" }, sub: [{ name: "/Z" }] }]
These items are just examples, the item count is 1000+ and the nested items can be unlimited too.
Any ideas how to do that?
You could do this with forEach and reduce methods and use one object to keep track of level based on the current part of the name property value.
const data = [{ name: "/X" }, { name: "/X/Y" }, { name: "/X2" }, { name: "/X2/Z" }, {name: '/X/K/1'}, {name: '/X/K/2'}]
const result = []
const level = {result}
data.forEach(({ name, ...rest }) => {
name.split('/').filter(Boolean).reduce((r, k) => {
if (!r[k]) {
r[k] = { result: [] }
r.result.push({
name: `/${k}`,
sub: r[k].result
})
}
return r[k]
}, level)
})
console.log(result)
Using reduce() and Map()
var data = [{ name: "/X" }, { name: "/X/Y" }, { name: "/X2" }, { name: "/X2/Z" }, { name: "/X/k" }]
var res = data.reduce((a, i) => {
let s = i.name.match(/\/\w+/g) || []
if (a.has(s[0])) {
let path = a.get(s[0])
i.name = s[1]
path.sub = path.sub || []
path.sub.push(i)
} else {
a.set(i.name, i)
}
return a
}, new Map())
console.log([...res.values()])

Filter the array with some criteria ES6

Hello guys I would like some code in ES6 javascript that solves this problem my data is an array of objects like the following
array = [
{
name: "SI",
value: 3
},
{
name: "MI",
value: 2
},
{
name: "SI",
value: 7
},
{
name: "SI",
value: 9
},
{
name: "MI",
value: 3
}
]
Just I want to take distinct object based on name with highest value like the following result
result = [
{
name: "SI",
value: 9
},
{
name: "MI",
value: 3
}
]
You can use array#reduce to accumulate the distinct name in an object accumulator. For each stored object, compare the value with the stored value and update in case it is less than the current value
let arr = [ { name:"SI", value:3 }, { name:"MI", value:2 }, { name:"SI", value:7 }, { name:"SI", value:9 }, { name:"MI", value:3 } ],
result = Object.values(arr.reduce((r,{name, value}) => {
r[name] = r[name] || {name, value: 0};
if(r[name].value < value)
r[name].value = value;
return r;
},{}));
console.log(result);
.as-console-wrapper {max-height: 100% !important; top: 0;}
Probably most efficient by reducing to an intermediate object first, and then mapping that object's entries:
const result = Object.entries(array.reduce((a, {name, value}) => {
a[name] = a[name] && a[name] >= value ? a[name] : value;
return a;
}, {})).map(([name, value]) => ({name, value}));
Complete snippet:
const array = [{
name: "SI",
value: 3
},
{
name: "MI",
value: 2
},
{
name: "SI",
value: 7
},
{
name: "SI",
value: 9
},
{
name: "MI",
value: 3
}
];
const result = Object.entries(array.reduce((a, {name, value}) => {
a[name] = a[name] && a[name] >= value ? a[name] : value;
return a;
}, {})).map(([name, value]) => ({name, value}));
console.log(result);

Transform the data

I have the following data structure:
const data = [
{
name: 'ABC',
salesData: [
{
timestamp: '2017-09-01',
value: 10
},
{
timestamp: '2017-09-02',
value: 2
}
]
},
{
name: 'DEF',
salesData: [
{
timestamp: '2017-09-01',
value: 8
},
{
timestamp: '2017-09-02',
value: 3
}
]
}
];
I would like to transform this to:
[
{
name: 'ABC',
'2017-09-01': 10,
'2017-09-02': 2
},
{
name: 'CDE',
'2017-09-01': 8,
'2017-09-02': 3
}
]
I'm trying to use Underscore's Chain and Map which I'm getting confused. So far I have the following, not sure how do I write the convertedSalesData to transform as per the need:
_.map(data, function(item) {
let name = item.name;
let salesData = item.salesData;
let convertedSalesData = ?
})
With ES6 you can use spread syntax ... to get this result.
const data = [{"name":"ABC","salesData":[{"timestamp":"2017-09-01","value":10},{"timestamp":"2017-09-02","value":2}]},{"name":"DEF","salesData":[{"timestamp":"2017-09-01","value":8},{"timestamp":"2017-09-02","value":3}]}]
var result = data.map(function({name, salesData}) {
return {name, ...Object.assign({}, ...salesData.map(({timestamp, value}) => ({[timestamp]: value})))}
})
console.log(result)
const data = [{
name: 'ABC',
salesData: [{
timestamp: '2017-09-01',
value: 10
},
{
timestamp: '2017-09-02',
value: 2
}
]
},
{
name: 'DEF',
salesData: [{
timestamp: '2017-09-01',
value: 8
},
{
timestamp: '2017-09-02',
value: 3
}
]
}
];
var res = data.map(function(a) {
var obj = {
name: a.name
};
a.salesData.forEach(function(x) {
obj[x.timestamp] = x.value;
})
return obj;
})
console.log(res);
Similar to #Nenad Vracar. I perfer to use 'reduce':
data.map(({ name, salesData }) => ({
name,
...salesData.reduce(
(record, { timestamp, value }) => {
record[timestamp] = value
return record
},
Object.create(null)
)
}))

Categories

Resources