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 6 years ago.
Improve this question
I have an object like this:
"data" :[{"question": "FirtName", "answer": "Daniel"},
{"question": "LastNane", "answer": "Daniel2"},
{"question": "Age", "answer": "80"}]
I want to change it to this format using java script
"data":
["FirtName" : "Daniel", "LastNane" : "Daniel2", "Age" : "80"]
Please, assist me!
I propose a solution for a input as an array of objects and an output as an object
var data = [{question: "FirtName", answer: "Daniel"},
{question: "LastNane", answer: "Daniel2"},
{question: "Age", answer: 80}
];
var result = {};
data.forEach(x => {
result[x.question] = x.answer;
});
console.log(result);
Keep attention to your object, thats what it should look about.
var object={
"data" :[
{
"question": "FirtName",
"answer": "Daniel"
},{
"question": "LastNane",
"answer": "Daniel2"
},{
"question": "Age",
"answer": "80"
}
]
};
var newObject={};
object.data.forEach(function(question){
newObject[question.question]=question.answer;
});
// {"FirtName" : "Daniel", "LastNane" : "Daniel2", "Age" : "80"}
console.log(newObject);
This code will get you the output you require
var object = {
"data": [{
"question": "FirtName",
"answer": "Daniel"
}, {
"question": "LastNane",
"answer": "Daniel2"
}, {
"question": "Age",
"answer": "80"
}]
};
var result = '"data":' + JSON.stringify(object.data.reduce(function (result, item) {
return result[item.question] = item.answer, result;
}, {})).replace('{', '[').replace('}', ']');
console.log(result);
// "data":["FirtName":"Daniel","LastNane":"Daniel2","Age":"80"]
You can use Array.prototype.reduce to convert to the object you desire - see demo below:
var array = [{"question": "FirtName","answer": "Daniel"},{"question": "LastNane","answer": "Daniel2"}, {"question": "Age","answer": "80"}];
var result = array.reduce(function(p, c) {
p[c.question] = c.answer;
return p;
}, {});
console.log(result);
Related
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 12 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Hello i have JSON response with structure like below, you can see "postID" and "judul" is inside one object with the parent is numeric array
[
{
"postID": [
1,
2,
3
],
"judul": [
"Tes",
"a",
"TesFront"
]
},
{
"postID": [
4,
5
],
"judul": [
"Testing",
""
]
},
]
What i want to do is simply just to make "postID" and "judul" into one new object with key named "newKey" with the value inside is an object with "postID" key and "judul" key you can see the different from previouse is now "postID" and "judul" is inside "newKey" object, like this:
[
{
"newKey": {
"postID": [
1,
2,
3
],
"judul": [
"Tes",
"a",
"TesFront"
]
},
},
{
"newKey":{
"postID": [
4,
5
],
"judul": [
"Testing",
""
]
}
},
]
Hope someone can help me to solve this.
Simplify the problem being presented. You effectively want to turn an array of this object:
{
"prop1": "val",
"prop2": "val2"
}
Into an array of this object:
{
"newprop": {
"prop1": "val1",
"prop2": "val2"
}
}
You can project an array into a new shape with .map. For example:
var newArr = arr.map(x => {
return {
newprop: {
prop1: x.prop1,
prop2: x.prop2
}
}
});
So yours might look something like this:
var newArr = arr.map(x => {
return {
namaBagian: x.namaBagian,
bagianID: x.bagianID,
newKey: {
postID: x.postID,
judul: x.judul
}
}
});
If the shape of the objects is more dynamic, you could potentially make clever use of the spread operator to not have to specify every value explicitly. But the general principle is the same. What you're looking to do is map an array into a new array, where each element takes on a new shape built from the data on the existing element.
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 do have json like below
[
[{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": {
"value": "2020-10-21T13:15:00"
},
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": {
"value": "2020-1-21T13:15:00"
},
"Date": "2020102",
"name": "xyz"
}
]
]
want to modified like below
[
{
"id": "4e181c1b-0a68-425e-9eb7-df36324b6cdb",
"date_actual": "2020-10-21T13:15:00",
"Date": "20201021",
"name": "abc"
},
{
"id": "4e181c1b-0a68-425e-9eb7-df36324qqq",
"date_actual": "2020-10-21T13:15:00",
"Date": "2020102",
"name": "xyz"
}
]
My service class code is
const rows = await bigqueryClient.query(queryData);
console.log("old json"+JSON.stringify(rows));
return rows;
how to solve this issue using nestjs any way please help me ?
This isn't specific to NodeJS or NestJS, but is regular old JavaScript. As you have an array of values you can use the Array.prototype.map method to map to a new array
return origArray.map((val) => ({ ...val, date_actual: val.date_actual.value }))
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 am new to ES6 so not sure if I am going about this in the correct way.
I have a JSON object of posts.
{
"title": "title one",
"category": ["dogs","cats","pets"]
},
{
"title": "title two",
"category": ["gold fish", "pets"]
}
I would like to get all the categories from the posts
["dogs","cats","gold fish", "pets"]
You could reduce all items of category and use a set for unique values. Then render an array of the set.
Method used:
Array#concat for concat arrays,
Array#reduce for getting all categories in one array,
Set for unique values,
spread syntax ... for getting a new array back.
var array = [{ title: "title one", category: ["dogs", "cats", "pets"] }, { title: "title two", category: ["gold fish", "pets"] }],
categories = [...new Set(array.reduce((r, a) => r.concat(a.category), []))];
console.log(categories);
Use Array.prototype.map to map over each post, and get each category.
let categories = posts.map((p) => p.category)
This might get you duplicate values, since you have "pets" in both category properties in those objects. To filter out duplicate instances, use Array.prototype.filter.
let categories = posts.map((p) => p.category).filter((c, i, arr) => i == arr.indexOf(c));
You can do this with map() method and Set and spread syntax ...
var data = [{"title":"title one","category":["dogs","cats","pets"]},{"title":"title two","category":["gold fish","pets"]}]
var result = [...new Set([].concat(...data.map(e => e.category)))]
console.log(result)
assuming that this is an array of objects:
var myArray = [{
"title": "title one",
"category": ["dogs", "cats", "pets"]
},
{
"title": "title two",
"category": ["gold fish", "pets"]
}
];
var newArray = myArray.map(function(x) {
return x.category;
});
alert(newArray);
You can use the map() function in order to project what you need, title in your case:
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 6 years ago.
Improve this question
I have a JSON file like this:
{[
{"name":"avc"},
{"name":"Anna"},
{"name":"Peter"},
{"Folder":[
{"name":"John"},
{"name":"Anna"},
{"Folder":[
{"name":"gg"},
{"name":"hh"}
]
}
]
}
]
}
It can be nested to any number of levels.
I want to traverse this file for any number of levels.
How can I achieve this using Javascript/JQuery
Thanks
You could iterate with Array#forEach and if a Folder is an array, then iterate that, too.
var a = [{ "name": "avc" }, { "name": "Anna" }, { "name": "Peter" }, { "Folder": [{ "name": "John" }, { "name": "Anna" }, { "Folder": [{ "name": "gg" }, { "name": "hh" }] }] }];
a.forEach(function iter(a) {
if (Array.isArray(a.Folder)) {
a.Folder.forEach(iter);
return;
}
console.log(a.name);
});
I read your question like this:
How can I iterate through an array of object, where an object may have
children of it's own. And every child may have children and so on...
One way is to write a recursive function, which is a function that calls itself. Here's a recursive function which will log every post into console.
Note that I've changed your model into a proper object. Also, I've added name to folder, but that can be removed if not necessary.
var a = [
{name:"avc"},
{name:"Anna"},
{name:"Peter"},
{name:"Folder name1",
folder:[
{name:"John"},
{name:"Anna"},
{name: "Folder name2",
folder:[
{name:"James"},
{name:"Clark"},
{name: "Folder name3",
folder:[
{name:"Cecilia"},
{name:"Clara"}
]},
{name:"Stephen"}
]
}
]
}
];
function read(obj){
for(var i = 0; i < obj.length; i++)
{
console.log(obj[i].name); // Or do what you need here...
if(obj[i].folder)
{
// Recursive call
read(obj[i].folder);
}
}
}
read(a);
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 7 years ago.
Improve this question
I am currently working with manipulating items in arrays/objects. I have not worked with lodash before but after reading how many features this utility tool offer decided to give it a go. The array I would like to modify, involves adding an addtional object sources and changing the keys. How can I achieve the below using lodash?
Current Array
var myArr =
[{
"flv": "myFile.flvs",
"mp4": "myFile.mp4",
"webm": "myFile.webm",
"thumbnail": "poster.png",
"title": "Test",
"id": 123456
}];
Desired result
{
data: [
{
sources: [{
file: "myFile.flv"
},{
file: "myFile.mp4"
},{
file: "myFile.webm"
}],
image: 'poster.png',
title: 'Test',
id: '123456'
},
....
]
}
How about this:
var myArr = {
"data": [{
"test1": "myFile.flv",
"test2": "myFile.mp4",
"test3": "myFile.webm",
"thumbnail": "poster.png",
"title": "Test",
"id": 123456
}]
};
var result = _.map(myArr.data, function(el) {
var sources = []
_.each(el, function(v, k) {
if (k.match(/test\d*/) !== null) {
sources.push({
file: v
});
}
});
return {
sources: sources,
image: el.thumbnail,
title: el.title,
id: el.id
};
});
console.log(result);
Result:
Update per OP request:
var EXTENSIONS = ['flv', 'mp4', 'webm'];
var myArr = [{
"flv": "myFile.flv",
"mp4": "myFile.mp4",
"webm": "myFile.webm",
"thumbnail": "poster.png",
"title": "Test",
"id": 123456
}];
var result = _.map(myArr, function(el) {
var sources = [];
_.each(el, function(v, k) {
if (_.contains(EXTENSIONS, k)) {
sources.push({
file: v
});
}
});
return {
sources: sources,
image: el.thumbnail,
title: el.title,
id: el.id
};
});
console.log(result);
Note: Here we can use _.contains (which is lodash) or EXTENSIONS.indexOf (which Javascript native). Since you want to learn lodash, I figure we use _.contains.
Update: Just removed .data from the map function because OP remove the data property from source data.