Restructure data from array with javascript [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 hope it is not a stupid question. My array looks like this:
const data = [
{
pollType: "1",
pollName: "Can you help me?",
options: [
{ Options: "Yes, I can", PID: "oFnxh-NDdcP" },
{ Options: "No way!", PID: "d9A10-omlUd" }
]
}
];
But I need to have it:
const result = [
{
pollType: "1",
pollName: "Can you help me?",
option1: "Yes, I can",
pid1: "oFnxh-NDdcP",
option2: "No way!",
pid2: "d9A10-omlUd"
}
];
Please don't get angry if it is so simple to do. I highly appreciate your help and if you do a an example so myself and other people can find it very useful in a future...

var orig = [{"pollType":"1","pollName":"Can you help me?","options":[{"Options":"Yes, I can","PID":"oFnxh-NDdcP"},{"Options":"No way!","PID":"d9A10-omlUd"}]}]
var newArr = [];
orig.forEach(v => {
var newObj = {};
newObj.pollType = v.pollType;
newObj.pollName = v.pollName;
v.options.forEach((k, i) => {
newObj["options" + (i + 1)] = k.Options;
newObj["pid" + (i + 1)] = k.PID;
});
newArr.push(newObj);
});
console.log(newArr);

Related

Convert array in array of objects [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 last month.
Improve this question
I have an array of words in a .json file
Like this:
[
"city",
"river",
"car"
]
And i want to get an object for each word
Something like this:
[
{
word: "city",
something: "..."
},
{
word: "river",
something: "..."
},
{
word: "car",
something: "..."
}
]
What is the best way to do this?
let new_array = [];
for(let item of old_array)
{
new_array.push({word:item, something:"..."});
}

How do I group muiltiple record in array to object in Javascript? [duplicate]

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 9 years ago.
Improve this question
Group javascript array items on value
Assuming you have a json object like:
[
{
prNumber: 20000401,
text: 'foo'
},
{
prNumber: 20000402,
text: 'bar'
},
{
prNumber: 20000401,
text: 'foobar'
},
]
Is it possible to perform a "join" on prNumber?
For example, maybe the desired output would be something like:
[
{
prNumber: 20000401,
text: [
'foo',
'foobar'
]
},
{
prNumber: 20000402,
text: [
'bar'
]
}
]
I have no code samples worth anything, so I will not post them here.
This would preferrably use vanilla javascript, but will accept a jQuery answer.
You should iterate the initial array and create new objects keyed off the prNumber. Here's how to do it using reduce (assuming you've assigned the array to a variable named orig):
var result = orig.reduce(function(prev, curr, index, arr) {
var num = curr["prNumber"];
if (!prev[num]) {
prev[num] = [];
}
prev[num].push(curr["text"]);
return prev;
}, {});
You can easily convert this into the example structure outlined in your question.

JSON Object to Arrays [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 4 years ago.
Improve this question
Below JSON Object needs to be iterated and need result as below.
[
{"Aazam":1, "Jagannath":2, "Bharath Kumar M":4 },
{"Bharath Kumar M":1, "Syad":1 },
{"Bharath Kumar M":2 }
]
output needed (can be in map):
Aazam: 1
Jagannath: 2
Bharath Kumar M: 4, 1, 2
Syad: 1
I tried with ES 6 syntax, I am able to get key and values but I am not successful in forming the array as needed.
Solution should be generic.
You can have a object as a output with the unique keys and values for each key as an array that holds the values of all similar keys.
var arr = [{
"Aazam": 1,
"Jagannath": 2,
"Bharath Kumar M": 4
},
{
"Bharath Kumar M": 1,
"Syad": 1
},
{
"Bharath Kumar M": 2
}
];
var res = arr.reduce((acc, item) => {
var keys = Object.keys(item);
keys.forEach((key) => {
if (!acc[key]) {
acc[key] = [];
}
acc[key].push(item[key]);
});
return acc;
}, {});
console.log(res);

How to convert two Array [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 4 years ago.
Improve this question
What is the best way to convert:
["Isolated-1", "SVT_FedPortGroup", "SVT_StoragePortGroup", "VM Network", "test-pg-2002"]
and this :
["target_Isolated-1", "target_SVT_FedPortGroup", "target_SVT_StoragePortGroup","target_VM Network" ,"target_test-pg-2002"];
to:
"NetworkMaps": [
{
"ENVID": null,
"SourcePG": "Isolated-1",
"TargetPG": "target_Isolated-1"
},
{
"ENVID": null,
"SourcePG": "VM Network",
"TargetPG": "target_SVT_FedPortGroup"
}...
]
I need to to merge two array with respective values.
For example
arr1 : ["a", "b", "c"];
arr2 : ["apple", "ball", "cat"];
result : [{source: "a",target: "apple"}, {source: "b",target: "ball"},{source: "c",target: "cat"}]
Just map them per index (if they have the same length) in a simple loop. Here is a demo:
a = ["Isolated-1", "SVT_FedPortGroup", "SVT_StoragePortGroup", "VM Network", "test-pg-2002"]
b = ["target_Isolated-1", "target_SVT_FedPortGroup", "target_SVT_StoragePortGroup", "target_VM Network", "target_test-pg-2002"]
c = {
"NetworkMaps": []
}
for (var i = 0; i < a.length; i++) {
c.NetworkMaps.push({
"ENVID": null,
"SourcePG": a[i],
"TargetPG": b[i]
})
}
console.log(c);

creating a dictionary from a javascript 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 5 years ago.
Improve this question
I got an array of objects of the following form:
let postArray = [
{
id = 1,
body = "some text"
},
{
id = 2,
body = "some other text"
}
]
which I want to rearange to the following form:
{
posts: {
1: {
id = 1,
body = "some text"
},
2: {
id = 2,
body = "some other text"
}
}
}
How can I do that using ES6?
You can reduce your array:
let postArray = [
{
id: 1,
body: "some text"
},
{
id: 2,
body: "some other text"
}
]
const posts = postArray.reduce((acc, b) => ({...acc, [b.id]: b}), {})
const result = { posts }

Categories