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)
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 this code,
"data": [
{
"pid": 1,
"status": "done",
"title": "a"
},
{
"pid": 1,
"status": "pending",
"title": "a"
},
{
"pid": 1,
"status": "pending",
"title": "21"
},
{
"pid": 2,
"status": "pending",
"title": "c"
}
]
I am trying to process it like this
"data": [
{
"pid": 1,
"done": 1,
"pending":2
},
{
"pid": 2,
"done": 0,
"pending":1
}
]
I have tried much to produce my desired results but no luck, now i m posting here to get some help.
thanks
You should be able to use Array.reduce for this purpose.
We create a map, keyed by pid then add 1 for each done or pending object.
Object.values will then turn this map into an array again.
I hope this helps you!
data = [
{
"pid": 1,
"status": "done",
"title": "a"
},
{
"pid": 1,
"status": "pending",
"title": "a"
},
{
"pid": 1,
"status": "pending",
"title": "21"
},
{
"pid": 2,
"status": "pending",
"title": "c"
}
]
let result = Object.values(data.reduce( (map, d) => {
if (!map[d.pid]) map[d.pid] = { pid: d.pid, done: 0, pending: 0 };
if (d.status === "done") map[d.pid].done++;
if (d.status === "pending") map[d.pid].pending++;
return map;
}, {} ));
console.log("Result:", result);
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")
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 years ago.
Improve this question
I have this JSON data with me and I want to get the value of slug from every child.
The main problem is I don't know how much children will be generated after every time I got new data. In short generation of a child inside the child is dynamic that is not fixed.
So, How can I get the value of slug from every child that is present in JSON data?
Here I write one JSON data:
[
{
"id": 11,
"title": "Bottle",
"__domenu_params": {},
"href": "www.products.com",
"target": "_blank",
"slug": "/undefined"
},
{
"id": 10,
"title": "Pencils",
"__domenu_params": {},
"slug": "/Pencils"
},
{
"id": 9,
"title": "Stationary",
"__domenu_params": {},
"slug": "/Stationary"
},
{
"id": 8,
"title": "Pen",
"__domenu_params": {},
"slug": "/Pen"
},
{
"id": 7,
"title": "Cable",
"__domenu_params": {},
"slug": "/Cable"
},
{
"id": 5,
"title": "Electronics",
"__domenu_params": {},
"slug": "/Electronics",
"children": [
{
"id": 4,
"title": "Charger",
"__domenu_params": {},
"slug": "/Charger"
},
{
"id": 3,
"title": "Laptop",
"__domenu_params": {},
"slug": "/Laptop"
},
{
"id": 2,
"title": "Mobile",
"__domenu_params": {},
"slug": "/Mobile",
"href": "www.electronics.com",
"target": "_blank",
"children": [
{
"id": 6,
"title": "Pendrive",
"__domenu_params": {},
"slug": "/Pendrive",
"href": "www.pendrive.com",
"target": "_self"
}
]
}
]
}
]
I tried this code and get the value of slug from this JSON only. What should I do to get for every possible JSON data?
let data = [{"id":11,"title":"Bottle","__domenu_params":{},"href":"www.products.com","target":"_blank","slug":"/undefined"},{"id":10,"title":"Pencils","__domenu_params":{},"slug":"/Pencils"},{"id":9,"title":"Stationary","__domenu_params":{},"slug":"/Stationary"},{"id":8,"title":"Pen","__domenu_params":{},"slug":"/Pen"},{"id":7,"title":"Cable","__domenu_params":{},"slug":"/Cable"},{"id":5,"title":"Electronics","__domenu_params":{},"slug":"/Electronics","children":[{"id":4,"title":"Charger","__domenu_params":{},"slug":"/Charger"},{"id":3,"title":"Laptop","__domenu_params":{},"slug":"/Laptop"},{"id":2,"title":"Mobile","__domenu_params":{},"slug":"/Mobile","href":"www.electronics.com","target":"_blank","children":[{"id":6,"title":"Pendrive","__domenu_params":{},"slug":"/Pendrive","href":"www.pendrive.com","target":"_self"}]}]}]
for (let i = 0; i< data.length ; i++) {
// console.log(data[i]["children"])
if (data[i]["children"]) {
console.log("inside")
for (let j = 0; j < data[i]["children"].length; j++) {
// console.log(data[i]["children"][j])
if (data[i]["children"][j]["children"]) {
console.log(data[i]["children"][j]["children"])
}
}
}
}
You can use Recursion to parse this data easily:
let data = [{"id":11,"title":"Bottle","__domenu_params":{},"href":"www.products.com","target":"_blank","slug":"/undefined"},{"id":10,"title":"Pencils","__domenu_params":{},"slug":"/Pencils"},{"id":9,"title":"Stationary","__domenu_params":{},"slug":"/Stationary"},{"id":8,"title":"Pen","__domenu_params":{},"slug":"/Pen"},{"id":7,"title":"Cable","__domenu_params":{},"slug":"/Cable"},{"id":5,"title":"Electronics","__domenu_params":{},"slug":"/Electronics","children":[{"id":4,"title":"Charger","__domenu_params":{},"slug":"/Charger"},{"id":3,"title":"Laptop","__domenu_params":{},"slug":"/Laptop"},{"id":2,"title":"Mobile","__domenu_params":{},"slug":"/Mobile","href":"www.electronics.com","target":"_blank","children":[{"id":6,"title":"Pendrive","__domenu_params":{},"slug":"/Pendrive","href":"www.pendrive.com","target":"_self"}]}]}]
function getAllSlugs(categories) {
// For each category...
return categories.reduce((slugList, category) => {
// add the slug for the particular category...
slugList.push(category.slug);
// and, check if the category has children...
if (category.children && category.children.length) {
// if children are there, call the same function with the
// children and add the slugs of children
slugList = slugList.concat(getAllSlugs(category.children));
}
return slugList;
}, []);
}
// Call the function
getAllSlugs(data);
Output:
[ '/undefined',
'/Pencils',
'/Stationary',
'/Pen',
'/Cable',
'/Electronics',
'/Charger',
'/Laptop',
'/Mobile',
'/Pendrive' ]
Hope this helps!
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
});