Change Json structure javascript [closed] - javascript

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
How do I change the format of the below json format to expected output:
[
{
"type": "System Usability Score",
"score": 74,
"date": "2020-03-19T18:30:00.000+0000"
},
{
"type": "System Usability Score",
"score": 87,
"date": "2020-03-31T18:30:00.000+0000"
}
]
Expected Output:
[
{
"name": "System Usability Score",
"series": [
{
"name": "2020-03-19T18:30:00.000+0000",
"value": 74
},
{
"name": "2020-03-31T18:30:00.000+0000",
"value": 87
}
]
}
]
Can someone please help?

Try like this with Array.prototype.reduce
"use strict";
const input = [
{
"type": "System Usability Score",
"score": 74,
"date": "2020-03-19T18:30:00.000+0000"
},
{
"type": "Net Promoter Score",
"score": 89,
"date": "2020-03-23T18:30:00.000+0000"
},
{
"type": "Net Promoter Score",
"score": 78,
"date": "2020-03-25T18:30:00.000+0000"
},
{
"type": "System Usability Score",
"score": 87,
"date": "2020-03-31T18:30:00.000+0000"
}
]
var output = input.reduce((collect, {type, score, date}) => {
let el, series
if (el = collect.find(({name}) => name === type)) {
series = el.series
} else {
series = collect[collect.push({name: type, series: []}) - 1].series
}
series.push({name: date, value: score})
return collect
}, [])
console.log(output)

Related

Need to format an object based on another Object data [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 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Using this Object which is a result from the API ( Columns are dynamic )
let json1 ={
"records": [
{
"ID": "1",
"Value": "A005",
},
{
"ID": "2",
"Value": "A007",
},
{
"ID": "3",
"Value": "B001",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
I need the below Object to format the data to represent in the UI
let json ={
"records": [
{
"ID": "1",
"Value": "<span style='color;red'>A005</span>",
},
{
"ID": "2",
"Value": "<span style='color;blue'>A007</span>",
},
{
"ID": "3",
"Value": "<span style='color;green'>B001</span>",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
Data inside conditional and records are dynamic. 'Value' can be any column name, it can also have any number of columns. All data are dynamic
We can do it via Array.forEach()
let json ={
"records": [
{
"ID": "1",
"Value": "A005",
},
{
"ID": "2",
"Value": "A007",
},
{
"ID": "3",
"Value": "B001",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
// PS: conditional format seems not elegant,especially []
let conditional = json.conditional[0].Value
json.records.forEach(e => {
let color = conditional[e.Value]
e.value = `<span style='color:${color}'>` + e.Value + `</span>`
})
console.log(json)

Compare between two objects and display rest of elememts in the 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 am having my data from API somethings like:
"data1": {
"items": [
{
"fields": {
"light_voltage": "17.8",
"light_current": "0.40",
"light_power": "7",
"fault_info": "-"
},
"id": 1715
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1716
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1717
}
],
"total": 02
}
And Another one is something likes:
"data2": {
"id": 1715,
"latitude": 2.13082,
"longitude": 119.32131,
}
Now i want to compare data1's "id" with data2's "id".
if data1's any of "id" match with data2's "id"'s
then it will displayed rest of values for data2's which are latitude and longitude.(In JavaScript)
Based on your original data, below is a simple reference for you,but I would suggest you to try it with your efforts
const data1 ={"data1": {
"items": [
{
"fields": {
"light_voltage": "17.8",
"light_current": "0.40",
"light_power": "7",
"fault_info": "-"
},
"id": 1715
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1716
},
{
"fields": {
"light_voltage": "17.1",
"light_current": "0.66",
"light_power": "11",
"fault_info": "-"
},
"id": 1717
}
],
"total":2
}
}
const data2 ={
"data2": {
"id": 1715,
"latitude": 2.13082,
"longitude": 119.32131
}
}
let contains = data1.data1.items.map(i => i.id).some(r => data2.data2.id==r)
if(contains){
console.log(data2.data2.latitude)
console.log(data2.data2.longitude)
}

How grupped array of objects on the property in another array? [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 have a json data. How can I group objects by dates so that the date goes as a new object, and data that has the same date are recorded in persons. Below is the source and desired view
Maybe someone will throw an example, and then I'll finish it myself.
This my source json
[
{
"Info": [
{
"age": "26"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "23"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "30"
}
],
"Date": "2020-08-15"
}
]
This my desired json
[
{
"name": "2020-08-14",
"persons": [
{
"Info": [
{
"age": "26"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "23"
}
],
"Date": "2020-08-14"
}]
},
{
"name": "2020-08-15",
"persons": [
{
"Info": [
{
"age": "30"
}
],
"Date": "2020-08-15"
}
]
}
]
Thank in advance
Using Array#reduce to accumulate the result-data. Iterating over the objects and look if is in the accumulated result object there exists a property with this date. If not create it and add a new object in it with the name-date and a person-property with an empty array. After this add in both cases the hole object to the person array.
At last get the Object#values to get the desired array out of the result-object.
let arr = [
{
"Info": [
{
"age": "26"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "23"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "30"
}
],
"Date": "2020-08-15"
}
];
let res = Object.values(arr.reduce((acc, cur) => {
if (acc[cur.Date]=== undefined) {
acc[cur.Date] = {name: cur.Date, persons: []};
}
acc[cur.Date].persons.push(cur);
return acc;
},{}));
console.log(res);

How to sort an array of objects by type? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I want to sort data based on a specific logic.
The rules for ordering are:
Type : Actual.
Type: Projected.
This is my code:
const myData = [{
"id": 1,
"businessEntityId": "BE001",
"financials": {
"Npbt": 2323,
"Interest": 123213,
"Depreciation": 213123,
"Ebit": 1312321,
"EbitDa": 123123,
"Leasing": 123213
},
"type": "Actual",
"startDate": "2018-06-15T00:00:00.000Z",
"endDate": "2018-06-15T00:00:00.000Z",
"model": "Commercial",
"duration": 12,
"quality": "Unqualified"
},
{
"id": 2,
"businessEntityId": "BE002",
"financials": {
"Npbt": 2323,
"Interest": 123213,
"Depreciation": 213123,
"Ebit": 1312321,
"EbitDa": 123123,
"Leasing": 123213
},
"type": "Projected",
"startDate": "2017-06-15T00:00:00.000Z",
"endDate": "2017-06-15T00:00:00.000Z",
"model": "Commercial",
"duration": 12,
"quality": "Projection"
},
{
"id": 3,
"businessEntityId": "BE002",
"financials": {
"Npbt": 2323,
"Interest": 123213,
"Depreciation": 213123,
"Ebit": 1312321,
"EbitDa": 123123,
"Leasing": 123213
},
"type": "Actual",
"startDate": "2017-06-15T00:00:00.000Z",
"endDate": "2017-06-15T00:00:00.000Z",
"model": "Commercial",
"duration": 12,
"quality": "Audited"
}
]
var order = {
model: {
Commercial: 2,
Agribusiness: 1
},
type: {
Historical: 3,
Actual: 2,
Projected: 1,
Proforma: 0
},
Actual: {
Unqualified: 3,
Qualified: 2,
Audited: 1
},
Projected: {
Projection: 3,
Qualified: 2,
Audited: 1
},
BOTTOM: Infinity
};
let result = myData.sort(
(a, b) => {
// eslint-disable-next-line no-unused-expressions
return order.model[a.model] - order.model[b.model] || order.type[a.type] - order.type[b.type]
}
);
console.log(result.map((obj) => obj.id))
So when I run the sort , the result is currently(check console for react app / result):
id 1, id 2, id 3.
I was expecting: 1,3,2?
How can I sort on Type respecting these rules and have type='Projected' at the bottom?

Iteration Javascript objects [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
Write a function that can take any number of episode ids as input and
returns all the information about those episodes.
After taking inputs from the user as multiple id numbers, it doesn't give output as the information about all episodes
let BigBang = {
"_embedded": {
"episodes": [
{
"id": 2913,
"name": "Pilot",
"season": 1,
"number": 1,
"airdate": "2007-09-24",
"airtime": "20:30",
"airstamp": "2007-09-25T00:30:00+00:00",
"runtime": 30,
"_links": {
"self": {
"href": "http:\/\/api.tvmaze.com\/episodes\/2913"
}
}
},
{
"id": 2914,
"name": "The Big Bran Hypothesis",
"season": 1,
"number": 2,
"airdate": "2007-10-01",
"airtime": "20:30",
"airstamp": "2007-10-02T00:30:00+00:00",
"runtime": 30,
"image": {
"medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg",
"original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg"
},
}
let id = prompt('Enter the episode ids');
let Info = (...id) => {
for(let current in BigBang._embedded.episodes) {
if(BigBang._embedded.episodes[current].id === parseInt(id)) {
let Detail = BigBang._embedded.episodes[current];
console.log(Detail);
}
}
}
Info(id);
let str = prompt('Enter the episode ids');
let idArray = JSON.parse("[" + str + "]");
let Info = (ids) => BigBang._embedded.episodes.filter(b => ids.includes(b.id));
console.log(Info(idArray));
After taking inputs from the user as multiple id numbers, it doesn't give output as the information about all episodes
Assuming you need to pass multiple ids, you can use the function filter along with the function indexOf (This is to convert to number and compare, of course, you can map that array first, Etc,. But that part is up to you).
This example receives the ids separated by comma and call the function Infousing the function apply to pass the entered ids as parameters (Like I said this is an example, in real execution you will pass the parameters directly to the function Info).
let BigBang = { "_embedded": { "episodes": [{ "id": 2913, "name": "Pilot", "season": 1, "number": 1, "airdate": "2007-09-24", "airtime": "20:30", "airstamp": "2007-09-25T00:30:00+00:00", "runtime": 30, "_links": { "self": { "href": "http:\/\/api.tvmaze.com\/episodes\/2913" } } }, { "id": 2914, "name": "The Big Bran Hypothesis", "season": 1, "number": 2, "airdate": "2007-10-01", "airtime": "20:30", "airstamp": "2007-10-02T00:30:00+00:00", "runtime": 30, "image": { "medium": "http:\/\/static.tvmaze.com\/uploads\/images\/medium_landscape\/4\/12369.jpg", "original": "http:\/\/static.tvmaze.com\/uploads\/images\/original_untouched\/4\/12369.jpg" }, } ] }};
let id = prompt('Enter the episode ids (separated by comma)');
let Info = (...ids) => {
var episodes = BigBang._embedded.episodes.filter(e => ids.findIndex(i => e.id == +i) !== -1);
console.log(episodes);
}
Info.apply(null, id.split(','));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Categories