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 an array like below:
let data:[
{
"class": "X",
"student":[
{
"name": "Bumblebee",
"id":"SAB77"
}
]
},
{
"class": "X",
"student":[
{
"name": "Omega",
"id":"SAB78"
}
]
},
{
"class": "IX",
"student":[
{
"name": "Pluto",
"id":"RBC17"
}
]
},
{
"class": "IX",
"student":[
{
"name":"16 psyche",
"id":"RBC18"
}
]
}
]
I want to group as following:
data:[
{
"class": "X",
"student":[
{
"name": "Bumblebee",
"id":"SAB77"
},
{
"name": "Omega",
"id":"SAB78"
}
]
},
{
"class": "IX",
"student":[
{
"name": "Pluto",
"id":"RBC17"
},
{
"name": "16 psyche",
"id":"RBC18"
}
]
}
]
let data = [
{
"class": "X",
"student":[
{
"name": "Bumblebee",
"id":"SAB77"
}
]
},
{
"class": "X",
"student":[
{
"name": "Omega",
"id":"SAB78"
}
]
},
{
"class": "IX",
"student":[
{
"name": "Pluto",
"id":"RBC17"
}
]
},
{
"class": "IX",
"student":[
{
"name":"16 psyche",
"id":"RBC18"
}
]
}
];
const output = data.reduce((acc, rec) => {
const obj = acc.find(ele => ele.class === rec.class);
if (obj) {
obj.student = [...obj.student, ...rec.student];
} else {
acc.push(rec);
}
return acc;
}, []);
console.log(output)
Try this.
let data = [
{
"class": "X",
"student": [
{
"name": "Bumblebee",
"id": "SAB77"
}
]
},
{
"class": "X",
"student": [
{
"name": "Omega",
"id": "SAB78"
}
]
},
{
"class": "IX",
"student": [
{
"name": "Pluto",
"id": "RBC17"
}
]
},
{
"class": "IX",
"student": [
{
"name": "16 psyche",
"id": "RBC18"
}
]
}
]
var reOrganized = [];
var unseen_classes = [];
for (var i = 0; i < data.length; i++) {
if (unseen_classes.indexOf(data[i].class) !== -1) {
for (var j = 0; j < reOrganized.length; j++) {
if (reOrganized[j].class === data[i].class) {
reOrganized[j].students.push(data[i].student[0])
}
}
}
else {
unseen_classes.push(data[i].class)
reOrganized.push({
class: data[i].class,
students: [data[i].student[0]]
})
}
}
console.log(reOrganized)
let data = [{
"class": "X",
"student": [{
"name": "Bumblebee",
"id": "SAB77"
}]
},
{
"class": "X",
"student": [{
"name": "Omega",
"id": "SAB78"
}]
},
{
"class": "IX",
"student": [{
"name": "Pluto",
"id": "RBC17"
}]
},
{
"class": "IX",
"student": [{
"name": "16 psyche",
"id": "RBC18"
}]
}
];
const result = data.reduce((acc, obj) => {
let existedObj = acc.length && acc.find(ele => ele.class === obj.class);
if (!acc.length || !existedObj) {
acc.push(obj);
return acc;
}
existedObj.student = [...existedObj.student, ...obj.student];
return acc;
}, []);
console.log(result);
Related
So in the API response example below, focusing on env_variables, I am trying grab the value for secret. I am stuck because as you can see, the name and value are not nested together. I am not familiar with how to grab the value based on the name in this example.
api response:
{
"id": 1146,
"job": {
"name": "jobname1",
},
"env_variables": [
{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
],
},
{
"id": 1147,
"job": {
"name": "jobname2",
},
"env_variables": [
{
"name": {
"name": "secret"
},
"value": {
"value": "10.13.7"
}
},
{
"name": {
"name": "test5"
},
"value": {
"value": "10.13.6"
}
},
],
}
js
jobs: []
apiEndpoint = "test.com/api"
fetch(this.apiEndpoint)
.then(response => response.json())
.then(body => {
for(let i=0; i<body.length; i++){
this.jobs.push({
'build_id': JSON.stringify(body[i].id),
'secret': //not sure how to pull the value (10.13.7)
})
}
})
You need nested loops, since there are two nested arrays: the top level of the response is an array of objects, and env_variables contains an array of objects.
fetch(this.apiEndpoint)
.then(response => response.json())
.then(body => {
for (let i = 0; i < body.length; i++) {
let env = body[i].env_variables;
for (let j = 0; j < env.length; j++) {
if (env[j].name.name == "secret") {
this.jobs.push({
'build_id': JSON.stringify(body[i].id),
'secret': env[j].value.value
})
}
}
}
})
You can do something like this inside .then(body=>...
const body = [{ //it looks like brackets [] were lost in OP
"id": 1146,
"job": {
"name": "jobname1",
},
"env_variables": [{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
{
"name": {
"name": "test1"
},
"value": {
"value": "10.13.6"
}
},
],
},
{
"id": 1147,
"job": {
"name": "jobname2",
},
"env_variables": [{
"name": {
"name": "secret"
},
"value": {
"value": "10.13.7"
}
},
{
"name": {
"name": "test5"
},
"value": {
"value": "10.13.6"
}
},
],
}
];
let secret = null;
body.forEach(b => {
let el = b.env_variables.find(e => e.name.name == 'secret');
if (el) { //found
secret = el.value.value;
return false; //exit forEach
}
});
console.log(secret);
You could also do something like this with Array.forEach and Array.find:
let data = [{ "id": 1146, "job": { "name": "jobname1", }, "env_variables": [{ "name": { "name": "test1" }, "value": { "value": "10.13.6" } }, { "name": { "name": "test1" }, "value": { "value": "10.13.6" } }, ], }, { "id": 1147, "job": { "name": "jobname2", }, "env_variables": [{ "name": { "name": "secret" }, "value": { "value": "10.13.7" } }, { "name": { "name": "test5" }, "value": { "value": "10.13.6" } }, ], } ]
let jobs = []
data.forEach(({id, env_variables}) => jobs.push({
build_id: id,
secret: ((env_variables.find(({name}) =>
name.name === 'secret') || {}).value || {}).value || 'N/A'
// ... other props
}))
console.log(jobs)
Assuming your result is an array, you could do something like this:
let secrets = results.reduce((result, item) => {
let secret = item["env_variables"].find((v) => {return v.name.name === "secret"})
if(secret){
result.push({id:item.id, secret: secret.value.value});
}
return result;
}, []);
This would return an array of objects like {id: 1, secret: ""} for each object in your result set that has a secret.
If you don't care whether the secret is present or not, you could modify the code slightly like this:
let secrets = results.reduce((result, item) => {
let secret = item["env_variables"].find((v) => {return v.name.name === "secret"})
result.push({id:item.id, secret: secret ? secret.value.value : ""});
return result;
}, []);
Which just leaves with you an empty string on the levels where there is no secret.
Check for the decimal id and group them accordingly.
Below are the sample and recommended JSON's
Sample JSON
{
"results": [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
},
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
},
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
},
{
"name": "Download",
"id": "4.2"
}
]
}
Would like to iterate and Re-structure the above JSON into below recommended format.
Logic: Should check the id(with and without decimals) and group them based on the number.
For Example:
1, 1.1, 1.2.3, 1.4.5 => data1: [{id: 1},{id: 1.1}....]
2, 2.3, 2.3.4 => data2: [{id: 2},{id: 2.3}....]
3, 3.1 => data3: [{id: 3},{id: 3.1}]
Recommended JSON
{
"results": [
{
"data1": [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
}
]
},
{
"data2": [
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
}
]
},
{
"data3": [
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
}
]
},
{
"data4": [
{
"name": "Download",
"id": "4.2"
}
]
}
]
}
I have tried the below solution but it doesn't group the object
var formatedJSON = [];
results.map(function(d,i) {
formatedJSON.push({
[data+i]: d
})
});
Thanks in advance.
You can use reduce like this. The idea is to create a key-value pair for each data1, data2 etc so that values in this object are the values you need in the final array. Then use Object.values to get those as an array.
const sampleJson = {"results":[{"name":"Download","id":"1.1.1"},{"name":"Download","id":"1.2"},{"name":"Download","id":"1.3.2"},{"name":"Download","id":"2"},{"name":"Download","id":"2.3"},{"name":"Download","id":"3.2"},{"name":"Download","id":"3.5"},{"name":"Download","id":"4.2"}]}
const grouped = sampleJson.results.reduce((a, v) => {
const key = `data${parseInt(v.id)}`;
(a[key] = a[key] || {[key]: []})[key].push(v);
return a;
},{});
console.log({results: Object.values(grouped)})
One liner / Code-golf:
let s={"results":[{"name":"Download","id":"1.1.1"},{"name":"Download","id":"1.2"},{"name":"Download","id":"1.3.2"},{"name":"Download","id":"2"},{"name":"Download","id":"2.3"},{"name":"Download","id":"3.2"},{"name":"Download","id":"3.5"},{"name":"Download","id":"4.2"}]},k;
console.log({results:Object.values(s.results.reduce((a,v)=>(k=`data${parseInt(v.id)}`,(a[k] = a[k]||{[k]:[]})[k].push(v),a),{}))})
Here you go:
var data = {
"results": [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
},
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
},
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
},
{
"name": "Download",
"id": "4.2"
}
]
};
let newSet = new Set();
data.results.forEach(e => {
let key = e.id.substring(0, e.id.indexOf('.'));
console.log(key);
if (newSet.has(key) == false) {
newSet.add(key);
newSet[key] = [];
}
newSet[key].push(e.id);
});
console.log(newSet);
Here's how you'd do it:
var data = {
"results": [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
},
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
},
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
},
{
"name": "Download",
"id": "4.2"
}
]
};
var newData = {
"results": {}
};
data.results.forEach(item => {
var num = item.id.slice(0, 1);
if (newData.results["data" + num]) {
newData.results["data" + num].push(item);
} else {
newData.results["data" + num] = [item];
}
})
data = newData;
console.log(data);
What this does is it iterates through each item in results, gets the number at the front of this item's id, and checks if an array of the name data-{num} exists. If the array exists, it's pushed. If it doesn't exist, it's created with the item.
let input = getInput();
let output = input.reduce((acc, curr)=>{
let {id} = curr;
let majorVersion = 'name' + id.split('.')[0];
if(!acc[majorVersion]) acc[majorVersion]= [];
acc[majorVersion].push(curr);
return acc;
},{})
console.log(output)
function getInput(){
return [
{
"name": "Download",
"id": "1.1.1"
},
{
"name": "Download",
"id": "1.2"
},
{
"name": "Download",
"id": "1.3.2"
},
{
"name": "Download",
"id": "2"
},
{
"name": "Download",
"id": "2.3"
},
{
"name": "Download",
"id": "3.2"
},
{
"name": "Download",
"id": "3.5"
},
{
"name": "Download",
"id": "4.2"
}
]
}
One solution with RegEx for finer control as it would differentiate easily between 1 and 11.
Also this will make sure that even if the same version comes in end(say 1.9 in end) it will put it back in data1.
let newArr2 = ({ results }) =>
results.reduce((acc, item) => {
let key = "data" + /^(\d+)\.?.*/.exec(item.id)[1];
let found = acc.find(i => key in i);
found ? found[key].push(item) : acc.push({ [key]: [item] });
return acc;
}, []);
I'm trying to rearrange an incoming JSON object to use in a React component.
The JSON object I'm receiving is jsonData & this is what my current code looks like:
const jsonData = {
"Jonas": {
"position": "CTO",
"employees": [{
"Sophie": {
"position": "VP Engineering",
"employees": [{
"Nick": {
"position": "Team Lead",
"employees": [{
"Pete": {
"position": "Backend Engineer",
"employees": []
}
},
{
"Barbara": {
"position": "Fronted Engineer",
"employees": []
}
}
]
}
},
{
"Melissa": {
"position": "Product Manager",
"employees": []
}
}
]
}
}]
}
}
const userList = [jsonData]
const formatData = list =>
list.map(item => {
let name, position, employees
for (let key in item) {
name = key
position = item[key].position
employees = item[key].employees ? item[key].employees : []
}
return {
name,
position,
employees: employees ? formatData(employees) : employees
}
})
console.log(formatData(userList))
I'm trying to add new id to each object & convert the jsonData to an array. I'm getting the output but can't add the id as follows -
[
{
"id": 0,
"name": "Jonas",
"position": "CTO",
"employees": [
{
"id": 1,
"name": "Sophie",
"position": "VP Engineering",
"employees": [
{
"id": 2,
"name": "Nick",
"position": "Team Lead",
"employees": [
{
"id": 3,
"name": "Pete",
"position": "Backend Engineer",
"employees": []
},
{
"id": 4,
"name": "Barbara",
"position": "Fronted Engineer",
"employees": []
}
]
},
{
"id": 5,
"name": "Melissa",
"position": "Product Manager",
"employees": []
}
]
}
]
}
]
How can I add an id to each object of the output?
You could use Object.assign with a closure over a counter for the id.
function convert(object) {
const fn = o => (name => Object.assign(
{ id: id++, name },
o[name],
{ employees: o[name].employees.map(fn)
}))(Object.keys(o)[0]);
var id = 0;
return fn(object);
}
var data = { Jonas: { position: "CTO", employees: [{ Sophie: { position: "VP Engineering", employees: [{ Nick: { position: "Team Lead", employees: [{ Pete: { position: "Backend Engineer", employees: [] } }, { Barbara: { position: "Fronted Engineer", employees: [] } }] } }, { Melissa: { position: "Product Manager", employees: [] } }] } }] } },
result = convert(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Simply add a var id and increment inside loop.
const jsonData = {
"Jonas": {
"position": "CTO",
"employees": [{
"Sophie": {
"position": "VP Engineering",
"employees": [{
"Nick": {
"position": "Team Lead",
"employees": [{
"Pete": {
"position": "Backend Engineer",
"employees": []
}
},
{
"Barbara": {
"position": "Fronted Engineer",
"employees": []
}
}
]
}
},
{
"Melissa": {
"position": "Product Manager",
"employees": []
}
}
]
}
}]
}
}
const userList = [jsonData]
var id=-1;
const formatData = list =>
list.map(item => {
let name, position, employees
for (let key in item) {
name = key
position = item[key].position
employees = item[key].employees ? item[key].employees : []
}
id=id+1;
return {
id,
name,
position,
employees: employees ? formatData(employees) : employees
}
})
console.log(formatData(userList))
I think this more better way and solve more nester employees array if add more
const jsonData = {
"Jonas": {
"position": "CTO",
"employees": [{
"Sophie": {
"position": "VP Engineering",
"employees": [{
"Nick": {
"position": "Team Lead",
"employees": [{
"Pete": {
"position": "Backend Engineer",
"employees": []
}
},
{
"Barbara": {
"position": "Fronted Engineer",
"employees": []
}
}
]
}
},
{
"Melissa": {
"position": "Product Manager",
"employees": []
}
}
]
}
}]
}
}
const userList = [jsonData]
const newFormat = list => {
findChildAndAddId(list, 0, list.length)
return list
}
const findChildAndAddId = (item, count, parentNumber) => {
if (item && Array.isArray(item)) {
item.map((value, index) => {
findChildAndAddId(value, count + index, item.length)
})
} else if (item) {
Object.keys(item).map(key => {
let value = item[key]
item.id = count
item.name = key
if (value.position) {
item.position = value.position
}
if (value.employees) {
item.employees = value.employees
}
delete item[key]
if (item.employees) {
findChildAndAddId(item.employees, count + parentNumber)
}
})
}
}
console.log(newFormat(userList))
I'm facing some issue in for loop while creating an object from array of object.I have an array as this in node js app:
[
{
"Material": "113/133",
"Name": [
{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [
{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
]
I want to return object like this which contains all the Material as array, Name and there value in array of object like this:
{
Material: ["113/133", "150/300"],
datasets: [
{
label: "WELD1",
data: [27520,1441]
},
{
label: "WELD2",
data: [676992,555]
},
{
label: "WELD3",
data: [100,20,0]
}
]
}
I want to get result using for loop.
you can use .reduce() and do something like this:
var arr = [
{
"Material": "113/133",
"Name": [
{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [
{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
];
var newArr = arr.reduce((acc, ob) => {
for (var key in ob)
if(typeof acc[key] === 'object')
acc[key] = acc[key] ? acc[key].concat(ob[key]) : [ob[key]];
else
acc[key] ? acc[key].push(ob[key]) : acc[key] = [ob[key]];
return acc;
}, {});
console.log(newArr);
let array = [
{
"Material": "113/133",
"Name": [
{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [
{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
]
let answer = {Material: [], datasets: []}
array.forEach(x => {
answer.Material.push(x.Material);
x.Name.forEach(na => {
let object = answer.datasets.find(obj => obj.label === na.name) || {label: "", data: []};
if(object.label === ""){
object.label = na.name;
object.data.push(na.value);
answer.datasets.push(object);
}else{
object.data.push(na.value)
}
});
});
console.log(answer);
The above is alternative solution using forEach instead of reduce
Use of Array.reduce to build your new data structure using data you have
const start = [{
"Material": "113/133",
"Name": [{
"name": "WELD1",
"value": 27520
},
{
"name": "WELD2",
"value": 676992
},
{
"name": "WELD3",
"value": 421
}
]
},
{
"Material": "150/300",
"Name": [{
"name": "WELD1",
"value": 1441
},
{
"name": "WELD2",
"value": 555
},
{
"name": "WELD3",
"value": 100992
}
]
}
];
const end = start.reduce((tmp, {
Material,
Name,
}) => {
// Handle the material
// If it do not exist in the array, push it
if (!tmp.Material.includes(Material)) {
tmp.Material.push(Material);
}
// Handle the datasets
// Look at each Name
Name.forEach(({
name,
value,
}) => {
// Can we find the label?
const labelFind = tmp.datasets.find(y => y.label === name);
// If we can't find the label, create a new dataset
if (!labelFind) {
tmp.datasets.push({
label: name,
data: [
value,
],
});
return;
}
// If we has found it push new value in the dataset
labelFind.data.push(value);
});
return tmp;
}, {
Material: [],
datasets: [],
});
console.log(end);
// This is the old fashioned way.
// Iterate over whole array,
// make a map, push value where 'name' is found in map
// later iterate over this map - dataMap - and form required datasets array.
var Material = [];
var dataMap = {};
arr.forEach(obj => {
Material.push(obj.Material);
obj.Name.forEach(item => {
if(dataMap[item.name]){
dataMap[item.name].push(item.value);
}
else {
dataMap[item.name] = [item.value];
}
});
});
var datasets = [];
Object.keys(dataMap).forEach(label => {
datasets.push({
label: label,
data: dataMap[label]
});
});
var result = {
Material: Material,
datasets: datasets
}
console.log(result);
I am having array in below format. i am having issue to append the value inside each chapter array. i need to add field like 'checked:true'. I need to get the data in same format. Expectation is, need same format with added one field inside chapter array.
{
"Books": [
{
"label":"Book1",
"data": [
{
"bookId": 3561,
"bookName": "AJ200",
"chapters": [
{
"id": 3926,
"name": "red"
},
{
"id": 3927,
"name": "yellow"
},
{
"id": 3928,
"name": "black"
}
]
}
]
},
{
"label":"Book2",
"data":[
{
"bookId": 3561,
"bookName": "AJ200",
"chapters": [
{
"id": 3564,
"name": "blue"
},
{
"id": 3565,
"name": "orange"
}
]
}
]
}
]
}
after adding field chapters array looks like as below,
"chapters": [
{
"id": 3564,
"name": "blue",
"checked":true
},
{
"id": 3565,
"name": "orange"
"checked":true
}
This is a little verbose but works all the same!
var myObject =
{
"Books": [
{
"label":"Book1",
"data": [
{
"bookId": 3561,
"bookName": "AJ200",
"chapters": [
{
"id": 3926,
"name": "red"
},
{
"id": 3927,
"name": "yellow"
},
{
"id": 3928,
"name": "black"
}
]
}
]
},
{
"label":"Book2",
"data":[
{
"bookId": 3561,
"bookName": "AJ200",
"chapters": [
{
"id": 3564,
"name": "blue"
},
{
"id": 3565,
"name": "orange"
}
]
}
]
}
]
}
var books = myObject.Books;
for(var i=0; i<books.length; i++) {
var bookData = books[i].data;
for(var j=0;j<bookData.length;j++) {
var chapters = bookData[j].chapters;
for(var k=0;k<chapters.length;k++) {
chapters[k].checked = true;
}
}
}
console.log(JSON.stringify(books));