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)
Related
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)
}
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 am having a json response like below. I want to fetch id(s) from all levels of children so that I can put them into an array and further passed on checkbox tree.
{
"status": "SUCCESS",
"status_code": "200",
"message": "Orgnization Hierarchy",
"responseBody": {
"data": {
"id": "2",
"parentId": "0",
"value": "Company1",
"children": [
// this is level 1 object 1
{
"id": "54",
"parentId": "2",
"value": "MasterCard",
"children": [
// this is level 2 of object 1
{
"id": "56",
"parentId": "54",
"value": "Branch bangalore",
"children": [
{
"id": "51",
"parentId": "56",
"value": "Area1",
"children": [
{ id:"66",
"parentId":"56",
value: "Deposite dept.",
// this is level 3 of object 1 and so on..
"children": [...] // further expanded
}
]
},
// this is level 1 of object 2
{
"id": "5",
"parentId": "54",
"value": "Branch pune",
"children": [...]
}
]
}
]
},
// level 1 object 2
{
"id": "74",
"parentId": "2",
"value": "Axis bank",
"children": [...] // further expanded
}
]
}
}
}
So, if I want to get id [in the example above like (2, 54, 56, 51, 66,...,5,...,74 )] from every level of the children, how can we achieve that? is there any recursive way or without recursive we can achieve it?
I processed below approach :
var arrayOfids;
function parsejson(){
for(var i = 0; i < data.length; i++){
var objectid = data[i].id
var objchildren = data[i].children;
getidfromchildren(objchildren);
}
}
function getidfromchildren(arr){
for(var j=0; j < arr.length; j++){
arrayOfids.push(arr[i].id);
if(arr[i].children.length != 0){
getidfromchildren(arr);
}
}
}
You could take Array#flatMap and get all id.
This approach assumes, that all children properties have at least an empty array.
If not, you need an array as default parameter, like
getId = ({ id, children = [] }) => [id, ...children.flatMap(getId)]
// ^^^^
const
getId = ({ id, children }) => [id, ...children.flatMap(getId)],
data = { status: "SUCCESS", status_code: "200", message: "Orgnization Hierarchy", responseBody: { data: { id: "2", parentId: "0", value: "Company1", children: [{ id: "54", parentId: "2", value: "MasterCard", children: [{ id: "56", parentId: "54", value: "Branch bangalore", children: [{ id: "51", parentId: "56", value: "Area1", children: [{ id: "66", parentId: "56", value: "Deposite dept.", children: [] }] }, { id: "5", parentId: "54", value: "Branch pune", children: [] }] }] }, { id: "74", parentId: "2", value: "Axis bank", children: [] }] } } },
ids = [data.responseBody.data].flatMap(getId);
console.log(ids)
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);
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)
This question already has answers here:
Why doesn't this arrow function work in IE 11?
(5 answers)
Javascript: Converting String to Number?
(4 answers)
Sort array of objects by string property value
(57 answers)
Closed 4 years ago.
I have an array with key-value pairs, array columns are id and name. I want to sort this array by id.
The id column value is of type string type but I want to sort them as numeric values also should work on IE
Here is my code:
var items = [{
"id": "165",
"name": "a"
},
{
"id": "236",
"name": "c"
},
{
"id": "376",
"name": "b"
},
{
"id": "253",
"name": "f"
},
{
"id": "235",
"name": "e"
},
{
"id": "24",
"name": "d"
},
{
"id": "26",
"name": "d"
}
];
console.log(items.sort((a, b) => Number(a.ID) - Number(b.ID)))
Though the order does change it doesn't change as expected also in IE an error is thrown.
Now you can do it with pure JS, using the sort method..
var items = [
{
"id": "165",
"name": "a"
},
{
"id": "236",
"name": "c"
},
{
"id": "376",
"name": "b"
},
{
"id": "253",
"name": "f"
},
{
"id": "235",
"name": "e"
},
{
"id": "24",
"name": "d"
},
{
"id": "26",
"name": "d"
}
];
items.sort((a,b)=>+a.id>+b.id);
console.log(items);