Convert JSON object list [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
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]}))};

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)

How to convert array of objects into 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 2 years ago.
Improve this question
I am trying to convert array of objects into a final output of objects. I have an array of objects as mentioned below:
[ { "id": "11", "value" : "9999","sample":"1"},
{ "id": "22", "value" : "8888","sample":"2"},
{ "id": "33", "value" : "7777","sample":"3"}
];
I need output as below:
{
{ "id": "11", "value" : "9999","sample":"1"},
{ "id": "22", "value" : "8888","sample":"2"},
{ "id": "33", "value" : "7777","sample":"3"}
}
can anyone help on this ?
Your Object is not valid in javascript. You can do the following:
const arr = [ { "id": "11", "value" : "9999","sample":"1"},
{ "id": "22", "value" : "8888","sample":"2"},
{ "id": "33", "value" : "7777","sample":"3"}
]
const obj = {}
arr.forEach(({id, value, sample}) => {
obj[id] = {value, sample}
})
console.log(obj)

hierarchical json data from flat json [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 3 years ago.
Improve this question
I have parent-child JSON data and I want to get all children (nested children) from a selected parent in the specific year-wise data format as below
var data = [{ "Id": "1", "Name": "abc", "EnteredOn": "03/01/2019", "attr": "abc" },
{ "Id": "2", "Name": "abcd", "EnteredOn": "02/01/2018", "attr": "abc" },
{ "Id": "3", "Name": "abcde", "EnteredOn": "04/01/2019", "attr": "abc" },
{ "Id": "4", "Name": "abcdef", "EnteredOn": "01/01/2016", "attr": "abc" }];
output:
{
"year": "2019",
"children": [
{
"ID": "1",
"Name": "abc",
"Date": "03/01/2019"
}
]
}
First, i recommend adding year attribute
data.forEach(a=> a['year'] = a.EnteredOn.getFullYear()
and using Lodash _.groupBy function
data.groupBy("year")

Dynamically node generation function [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 8 years ago.
Improve this question
var v = [
{
"message": {
"code": "S200",
"message": "Success"
},
"data": {
"car": [
{
"id": 1,
"val": "Toyota"
},
{
"id": 2,
"val": "Honda"
},
{
"id": 3,
"val": "Nissan"
}
],
"truck": [
{
"id": 1,
"val": "Benz"
},
{
"id": 1,
"val": "Volvo"
}
]
}
}
]
I have a dynamic json shown like above.
how could get v[0].data.car node dynamically(without using v[0], v[1] etc) in undescorejs or angularjs. Is Hashmap is possible?.
You need to iterate through the array in angularjs, like this
v.forEach(function(element){
console.log(element.data.car[0]);//Over here you can again iterate the car array, I have used the first element instead
});

If an array has an attribute, find the value of another attribute of the same 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 8 years ago.
Improve this question
"dTableRowData": [
{
"id": "1",
"rowData": [
"tt",
"Sep13, 2010"
],
"action": [
{
"hrefvar": "aaa",
"label": "fff"
},
{
"hrefvar": "bbb",
"label": "Details"
},
{
"hrefvar": "ccc",
"label": "View"
}
]
}
]
Above is the array I have, and I need to find the value of "action" by taking the "id" (if action is present). Please help me achieve this using jQuery.
This question is closed
I think you need something like bellow. You have a JSON like bellow.
var data = {"dTableRowData": [
{
"id": "1",
"rowData": [
"tt",
"Sep13, 2010"
],
"action": [
{
"hrefvar": "aaa",
"label": "fff"
},
{
"hrefvar": "bbb",
"label": "Details"
},
{
"hrefvar": "ccc",
"label": "View"
}
]
}
]}
You want to get an action for every object in dTableRowData. I added curly braces to dTableRowData other wise it will throw error.
So this is what worked for me using Javascript.
var actions = data.dTableRowData.map(function (obj) {
// Get each object in data.dTableRowData and get action according to it's id from object
return obj.action[obj.id];
});
console.log(actions);
First of all it has got nothing to do with jQuery.
Second, its a syntax error.
You need to wrap this in { and } to make it valid.
You can use this tool to check the validity of your JSON.
Next, this is how you do it. I've validated the JSON and added dummy data so that there is one element that doesn't have action attribute.
var array ={
"dTableRowData": [
{
"id": "1",
"rowData": [
"tt",
"Sep13, 2010"
],
"action": [
{
"hrefvar": "aaa",
"label": "fff"
},
{
"hrefvar": "bbb",
"label": "Details"
},
{
"hrefvar": "ccc",
"label": "View"
}
]
},
{
"id": "3",
"rowData": [
"tt",
"Sep13, 2010"
],
"action": [
{
"hrefvar": "aaa",
"label": "fff"
},
{
"hrefvar": "bbb",
"label": "Details"
},
{
"hrefvar": "ccc",
"label": "View"
}
]
},
{
"id": "2",
"rowData": [
"tt",
"Sep13, 2010"
]
}
]
};
This is a function that returns all the IDs that have actions
function getRequiredIDs(myArray){
var ids = [];
for (var tableRowData in myArray.dTableRowData){
var elements = myArray.dTableRowData[tableRowData];
if(elements.action!=null){
ids.push(elements.id);
}
}
return ids;
}
And this is how you use it
var ids = getRequiredIDs(array);
var result = ""
for(var i=0;i<ids.length;i++){
result+=ids[i]+" ";
}
alert("IDs that have actions are - "+result);
Here is the fiddle
EDIT:
If you want to get actions for specific id, you need to change the code a bit like following.
function getRequiredActions(_id,myArray){
for (var tableRowData in myArray.dTableRowData){
var elements = myArray.dTableRowData[tableRowData];
if(elements.id == _id){
if(elements.action != null){
return elements.action;
} else {
return false;
}
}
}
}
And this is how you use it.
var actions = getRequiredActions("1",array);
console.log(actions);
Final fiddle

Categories

Resources