How to make object from two array in javascript [closed] - javascript

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.

Related

javascript check if any array is empty from an object [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 1 year ago.
Improve this question
There is an object
let response = {
"meta": {
"id": 781
},
"content": {
"title": "Test",
"min": "1",
"max": "30",
"series": [
{
"name": "A",
"data": []
},
{
"name": "B",
"data": [3, 5, 6]
}
]
}
}
I need to check if any array of key, data is an empty array from series array. So, basically
if any data is empty array from series {
// do something
}
How can I achieve that?
you can try this one
let temp = response.content.series;
let num_of_empty_arr=0;
for(let i=0;i<temp.length;i++){
let obj=temp[i];
if(obj.data.length==0)
num_of_empty_arr++;
}
console.log("totol empty arrays",num_of_empty_arr);
you can do this by looping through response.content.series
for(let i of response.content.series){
if(i.data.length === 0){
//do something
console.log(i.name)
}
}
some allows you to return a boolean (true or false) based on a condition. Use it on the series data and return a value based upon whether the data array has any elements.
let response={meta:{id:781},content:{title:"Test",min:"1",max:"30",series:[{name:"A",data:[]},{name:"B",data:[3,5,6]}]}};
let response2={meta:{id:781},content:{title:"Test",min:"1",max:"30",series:[{name:"A",data:[1,2]},{name:"B",data:[3,5,6]}]}};
function areSomeEmpty(data) {
return data.content.series.some(arr => !arr.data.length);
}
console.log(areSomeEmpty(response));
console.log(areSomeEmpty(response2));

Restructure object of object to an json data [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have the following JSON data like this i tried iterating it with object.entries
{
"1245": {
"case_id": "1245",
"nodes": 11,
"others": {
"hops": "1"
},
"status": "COMPLETE"
}
}
I would like it to be reconstructed in the following JSON format using javscript:
expected output like :
{
data :[
case_id:'123",
nodes:11,
status: "COMPLETE",
hops:1,
]
}
The fact that your data is structured as a key/value pair, means that you will need to use Object.keys to iterate over the data before you are able to map it into an array. You will then need to construct your object so that it has "data" as the property for the mapped array.
const yourData = { "1245": {
"case_id": "1245",
"nodes": 11,
"others": {
"hops": "1"
},
"status": "COMPLETE"
}
}
const theArrayYouWant = Object.keys(yourData).map((entry) => {
return {
case_id: yourData[entry].case_id,
nodes: yourData[entry].nodes,
status: yourData[entry].status,
hops: yourData[entry].others.hops,
};
});
const yourDataType = { data: theArrayYouWant };

How to design nested 3 level 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 an object like below:
Parent: { Child1: [ {name:'grandchild1', value:'abc', checked:true}, {name:'grandchild2', value:'pqr', checked:false} ], Child2: [ {name:'grandchild3', value:'abcd', checked:false}, {name:'grandchild4', value:'pqrs', checked:true} ], parent2{...........}.... };
How can I make it nested JSON.
Just like in root: parent1, parent2...
Child: children1, ....( Corresponding to parent)
Grandchildren: based on children
Please guide me how can I make it?
To make a JSON object you just have to follow JSON syntax
In your example it will look like:
{
"parent": {
"child1": [{
"name": "grandchild1",
"value": "abc",
"checked": "true"
}, {
"name": "grandchild2",
"value": "pqr",
"checked": "false"
}]
}
}
Please note that in JSON, string values must be written with double quotes.
If you need to get JSON string from object programmatically, you can write in javascript:
var myJSON = JSON.stringify(obj);

Parsing nested array objects in json using Java script/jQuery [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 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);

Manipulating and changing keys of original array [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 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.

Categories

Resources