Merge objects of single array on basis of object key [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 days ago.
Improve this question
I'm confused to merge array contents from JavaScript.
Please help to get the best way to get desired result. A i also have tried other ways referred on SO.
[
{
"Afghanistan": 2646432,
"categories": "2018"
},
{
"Afghanistan": 6545700,
"categories": "2019"
},
{
"Afghanistan": 7475886,
"categories": "2020"
},
{
"Albania": 740232,
"categories": "2018"
},
{
"Albania": 2374785,
"categories": "2019"
},
{
"Albania": 2180850,
"categories": "2020"
}
]
This was my expected output::
[ { "Afghanistan": 2646432, "Albania": 740232, "categories": "2018" }, { "Afghanistan": 6545700, "Albania": 2374785, "categories": "2019" }, { "Afghanistan": 7475886, "Albania": 2180850, "categories": "2020" } ]

array.reduce((acc, item) => {
const found = acc.find(obj => obj.categories === item.categories);
if (found) {
Object.assign(found, item);
} else {
acc.push(item);
}
return acc;
}, [])

let list=[{Afghanistan:2646432,categories:"2018"},{Afghanistan:6545700,categories:"2019"},{Afghanistan:7475886,categories:"2020"},{Albania:740232,categories:"2018"},{Albania:2374785,categories:"2019"},{Albania:2180850,categories:"2020"},{India:2029282726,categories:"2022"},{India:2029282729,categories:"2023"},{Afghanistan:2029282729,categories:"2023"}];
let result = Object.values(list.reduce((acc, cur) => (acc[cur.categories] ? acc[cur.categories] = {...acc[cur.categories], ...cur} : acc[cur.categories] = cur, acc), {}));
console.log(result);
You can use reduce method

Related

How to convert array to single object? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have array object like this.
[
{
"name": "name1",
"type": "type1",
"car": "car1",
"speed": 1
},
{
"name": "name1",
"type": "type1",
"car": "car2",
"speed": 2
},
]
And i want to convert it single object for prevent data replay.
How can i do this?
{
"name": "name1"
"type": "type1",
"car": [
"car1",
"car2",
],
"speed": [
1,
2,
],
}
Could someone help me? Thanks.
If name and type can have same value and car and speed various,then below is a reference for you
Note: in order to get an expected answer(solution),you need to make you question more clear,the most important thing is the value of which properties various
const data = [
{
"name": "name1",
"type": "type1",
"car": "car1",
"speed": 1
},
{
"name": "name1",
"type": "type1",
"car": "car2",
"speed": 2
},
]
const result = data.reduce((a,v) =>{
let obj = a.find(i => i.name == v.name)
if(obj){
obj.car.push(v.car)
obj.speed.push(v.speed)
}else{
a.push({'name':v.name,'type':v.type,'car':[v.car],'speed':[v.speed]})
}
return a
},[])
console.log(result)

Convert JSON object list [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
i need to convert JSON object to a specific list. My JSON looks like this:
{
"data": {
"data_first": {
"2014-03-14T10:07:31": [
{
"name": "name"
},
{
"name": "name"
}
],
"2014-03-14T10:08:30": [
{
"name": "name"
},
{
"name": "name"
}
]
}
}
}
and what i need is:
{
"data": [
{
"date": "2014-03-14T10:07:31",
"values": [
{
"name": "name"
},
{
"name": "name"
}
]
},
{
"date": "2015-03-14T10:09:31",
"values": [
{
"name": "name"
},
{
"name": "name"
}
]
}
]
}
I need an array of objects, where the data should be an object value as my example below. Can you help?
You can use this in javascript.
const data = your json object.data;
const result = {data: Object.keys(data.data_first).map(key => ({date: key, values: data.data_first[key]}))};

How to extract all first objects for an object of array? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I make an axios request like that :
async get() {
await this.$axios.get(`my_api_url`)
.then(response => {
this.data = response.data;
}).catch(() => { console.log('error') })
},
It returns a response with these data:
{
"Apple": [
{
"id": 26,
"name": "Apple",
"date_in": "2020-07-01"
},
{
"id": 23,
"name": "Apple",
"date_in": "2020-06-01"
}
],
"Cherry": [
{
"id": 24,
"name": "Cherry",
"date_in": "2020-06-01"
}
],
"Banana": [
{
"id": 25,
"name": "Banana",
"date_in": "2020-06-01"
}
]
}
I would like to be able to browse this object and keep only the first object in each table. Then put the selected objects in a new table.
Is it possible to do this?
function GetObjects(input){
var output = [];
for(let key in input){
output.push(input[key][0]);
}
return output;
}
If I understand your question correctly
Like that?
const src = {"Apple":[{"id":26,"name":"Apple","date_in":"2020-07-01"},{"id":23,"name":"Apple","date_in":"2020-06-01"}],"Cherry":[{"id":24,"name":"Cherry","date_in":"2020-06-01"}],"Banana":[{"id":25,"name":"Banana","date_in":"2020-06-01"}]},
resultArr = Object.values(src).map(([o]) => o)
console.log(resultArr)
.as-console-wrapper{min-height:100%;}
Or, like that?
const src = {"Apple":[{"id":26,"name":"Apple","date_in":"2020-07-01"},{"id":23,"name":"Apple","date_in":"2020-06-01"}],"Cherry":[{"id":24,"name":"Cherry","date_in":"2020-06-01"}],"Banana":[{"id":25,"name":"Banana","date_in":"2020-06-01"}]},
resultObj = Object.entries(src).reduce((r,[key,[o]]) =>
(r[key] = o,r),{})
console.log(resultObj)
.as-console-wrapper{min-height:100%;}
You can get the results like:
const data = {
"Apple": [
{
"id": 26,
"name": "Apple",
"date_in": "2020-07-01"
},
{
"id": 23,
"name": "Apple",
"date_in": "2020-06-01"
}
],
"Cherry": [
{
"id": 24,
"name": "Cherry",
"date_in": "2020-06-01"
}
],
"Banana": [
{
"id": 25,
"name": "Banana",
"date_in": "2020-06-01"
}
]
}
const results = Object.keys(data).filter(item => data[item][0]).map(item => data[item][0]);
console.log(results);

print object in a array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a api with json like this:
{
"data": [
{
"carrier" : "abc",
"extra": {
"date": "1970-01-01"
},
}
{
"carrier" : "abc",
"extra": {
"date": "1970-01-01"
},
}
]
}
Then how can I use date and carrier in a map function.
I am using reactjs
const obj = {
"data": [
{
"carrier" : "abc",
"extra": {
"date": "1970-01-01"
},
}
{
"carrier" : "abc",
"extra": {
"date": "1970-01-01"
},
}
]
}
const newArray = object.data.map(ele => {
return {
carrier: ele.carrier,
date: ele.extra && ele.extra.date
};
})
// Result
[
{ carrier: "abc", date: "1970-01-01" },
{ carrier: "abc", date: "1970-01-01" }
]
const dataObj = {
data: [
{
carrier: "abc",
extra: {
date: "1970-01-01"
}
},
{
carrier: "abc",
extra: {
date: "1970-01-01"
}
}
]
};
const dateArr = dataObj.data.map(obj => {
return {
date : obj.extra?.date,
carrier : obj.carrier,
}
});
dateArr
[
{ carrier: "abc", date: "1970-01-01" },
{ carrier: "abc", date: "1970-01-01" }
]

JavaScript: How can I transform objects array A to objects array B in the efficiency way? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I got object array A as this:
[
{
"Timestamp": "2015-10-01 00:00:00",
"Label": "Voltage",
"Value": "230.12"
},
{
"Timestamp": "2015-10-01 00:00:00",
"Label": "Frequency",
"Value": "50.12"
},
{
"Timestamp": "2015-10-01 00:00:00",
"Label": "Power",
"Value": "23"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Label": "Voltage",
"Value": "231.12"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Label": "Frequency",
"Value": "51.12"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Label": "Power",
"Value": "23.4"
}
]
I want to transform to object array B as this:
[
{
"Timestamp": "2015-10-01 00:00:00",
"Voltage": "230.12",
"Frequency": "50.12",
"Power": "23"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Voltage": "231.12",
"Frequency": "51.12",
"Power": "23.4"
}
]
I was looping to get the timestamp and re-loop again to get label and value to form new object array. It works but when the data become few hundred thousands object array then it's not efficiency and crash the browser. Would someone can think of a better way please. Thanks very much.
JSON is just a transport/storage format. It's generally not something you should attempt to manipulate directly. Best to manipulate the actual JavaScript objects/arrays.
If you can guarantee the original array sort order, try something like this:
var outputArray = [];
var currentObj = null;
originalArray.forEach(function (measurement) {
if (!currentObj || currentTimestamp !== measurement.Timestamp) {
currentTimestamp = measurement.Timestamp;
currentObj = {
Timestamp: measurement.Timestamp;
}
outputArray.push(currentObj);
}
currentObj[measurement.Label] = measurement.Value;
});
Then, you can just loop through and build the new array as you go.
As your desired output showed ignoring the time of the timestamp this will trim that part of the timestamp - if that was a mistake in the question I think you should be able to remove that part by yourself
var b = a.reduce((acc, cur) => {
var strippedTimestamp = cur.Timestamp.substring(1, 10) + " 00:00:00";
var obj = acc.find(e => e.Timestamp == strippedTimestamp);
if (!obj) {
obj = { Timestamp: strippedTimestamp };
acc.push(obj);
}
obj[cur.Label] = cur.Value;
return acc;
}, []);
will output
[
{
"Timestamp": "015-10-01 00:00:00",
"Voltage": "230.12",
"Frequency": "50.12",
"Power": "23"
},
{
"Timestamp": "015-10-02 00:00:00",
"Voltage": "230.12",
"Frequency": "50.12",
"Power": "23"
}
]

Categories

Resources