How to convert object to array of objects 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 7 years ago.
Improve this question
I have the following object data which looks like this:
data = {
anna:{
phase1:23,
phase2:24,
phase3:0,
phase4:5,
phase5:0
},
Robin:{
phase1:16,
phase2:12,
phase3:21,
phase4:23,
phase5:2
}
}
Now I wanted to convert them to where data variable is object and anna and robin is array of objects:
data = {
anna:[
{ phase1: 23 },
{ phase2: 24 },
{ phase3: 0 },
{ phase4: 5 },
{ phase5: 0 }
],
Robin:[
{ phase1: 16 },
{ phase2: 12 },
{ phase3: 21 },
{ phase4: 23 },
{ phase5: 2 }
]
}

A function like below might help you:
EDIT: edited the answer accommodating changes suggested by cafebabe1991.
function convertToArray(obj) {
var retVal = [];
for (var key in obj) { { //iterates through the list of key-val pairs
if (obj.hasOwnProperty(key)) {
retVal.push({ key: obj[key]}); //pushes it to newly created array
}
}

try this
function convertToArray(obj) {
var retVal = [];
for (key in obj) { //iterates through the list of key-val pairs
if (obj.hasOwnProperty(key)) {
retVal.push({ key: obj[key]});
} //pushes it to newly created array
}
return retVal;
}

Related

Combine Objects in a js array without overwriting them [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 7 months ago.
Improve this question
i have an array like below
[
{
"202229": "8.418"
},
{
"202229": null
},
{
"202229": null
},
{
"202230": "10.713"
},
{
"202230": "0.859"
}
]
i want to convert it to below structure
[
{
"202229": "8.418",
"202229": null,
"202229": null,
"202230": "10.713",
"202230": "0.859"
}
]
Note that values are not overwritten and keys "202229" etc are dynamic . I tried using reduce methods but i couldn't get it in this format . Any help would be appreciated . Thanks
You could disperse the keys to various objects.
const
data = [{ 202229: "8.418" }, { 202229: null }, { 202229: null }, { 202230: "10.713" }, { 202230: "0.859" }, { 202231: "0.503" }, { 202231: null }],
result = data.reduce((r, o) => {
Object.entries(o).forEach(([k, v]) => {
let i = 0;
while (k in (r[i] ??= {})) i++;
r[i][k] = v;
});
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Dynamically assign object values to variable in JavaScript [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
I need to assign the values of the below array of objects into defined variables which are initialized as empty strings.
What I have tried up until now:
const transaction = [{
number: 10,
value: "Ten"
},
{
number: 20,
value: "Twenty"
},
];
let transactionOneValue, transactionTwoValue, transactionOneNumber, transactionTwoNumber = "";
if (transaction.length > 0) {
transaction.forEach(item => {
[transactionOneNumber, transactionTwoNumber].forEach(num => num = item.number);
[transactionOneValue, transactionTwoValue].forEach(val => val = item.value);
});
}
Expected output:
transactionOneValue = "Ten",
transactionTwoValue = "Twenty",
transactionOneNumber = 10,
transactionTwoNumber = 20
How can I do it?
Destructuring seems to be simplest approach here, but I don't see how this would be useful. Creating separate variables for every item in a list isn't scalable.
let [
{number: transactionOneNumber, value: transactionOneValue},
{number: transactionTwoNumber, value: transactionTwoValue}
] = transaction
const transaction = [{
number: 10,
value: "Ten"
},
{
number: 20,
value: "Twenty"
},
];
let transactions = [];
if (transaction.length > 0) {
transaction.map((value, index, array) => {
for(x of Object.values(value)) {
transactions.push(x);
}
})
}
let transactionOneNumber = transactions[0],
transactionOneValue = transactions[1],
transactionTwoNumber = transactions[2],
transactionTwoValue = transactions[3];
console.log(transactionOneNumber);
console.log(transactionOneValue);
console.log(transactionTwoNumber);
console.log(transactionTwoValue);
output:
10
"Ten"
20
"Twenty"
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values

re-create an Object to be 2D 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 2 years ago.
Improve this question
I have an object like this
{ '2020-08': {Invoice: 21400, Receipt: 20800, ...},
'2020-09': {Invoice: 8003.6, ...},
'2020-10': {Receipt: 7779.2, ...},
'2020-11': {Invoice: 32970, ...}
}
Wanna make it to be 2d array like this
[ ['2020-08', '2020-09', '2020-10', '2020-11'],
[ 21400 , 8003.6 , 0 , 32970 ], //Invoice
[ 20800 , 0 , 7779.2 , 0 ], //Receipt
...
]
It is very easy to achieve using Object.keys.
const data = {
'2020-08': {
Invoice: 21400,
Receipt: 20800
},
'2020-09': {
Invoice: 8003.6,
Receipt: 7779
},
'2020-10': {
Receipt: 7779.2,
Invoice: 32970
},
'2020-11': {
Invoice: 32970,
}
}
const res = []
const keys = [...Object.keys(data)]
const d = Object.keys(data).map(item => {
return data[item]["Invoice"] || 0
})
const d1 = Object.keys(data).map(item => {
return data[item]["Receipt"] || 0
})
console.log([keys, d, d1])
Try:
let result = [], key= [], invoice= [], receipt = [];
Object.keys(obj).forEach((x) => {
key.push(x);
invoice.push(obj[x].Invoice || 0);
receipt.push(obj[x].Receipt || 0);
});
result = [key,invoice,receipt];

how to merge similar key values in same keys in javascript [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 2 years ago.
Improve this question
var array = [{
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "R"
}, {
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "E"
}, {
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "N"
}];
I want it to be like this
[{
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "R",
"E",
"N"
}]
I want this in javascript. can anyone help me
thanks in advance
Current example does not check for duplicates REV_TYPE:
const array = [
{ ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'R' },
{ ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'E' },
{ ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'N' },
{ ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'N' },
{ ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'N' },
{ ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'N' },
];
const result = array.reduce((acc, val) => {
const findType = acc.find(t => t.ADJUSTMENT_TYPE === val.ADJUSTMENT_TYPE);
if (findType) {
findType.REV_TYPE.push(val.REV_TYPE);
} else {
acc.push({ ADJUSTMENT_TYPE: val.ADJUSTMENT_TYPE, REV_TYPE: [val.REV_TYPE] });
}
return acc;
}, []);
console.log(result);

Vue JS create json objects from one json 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 3 years ago.
Improve this question
problem is that. I have one json object, lets assume it's look like that:
{
"first": {
"arg": [
"val1",
"val2"
]
},
"second": {
"arg1": [
"val11",
"val12"
],
"arg2": [
"val21",
"val22"
]
}
}
And now I need to make from it two objects , looking like this:
{
"first": {
"arg":"val1"
},
"second": {
"arg1":"val11",
"arg2":"val21"
}
}
And second one like this:
{
"first": {
"arg":"val2"
},
"second": {
"arg1":"val12",
"arg2":"val22"
}
}
There is easy way to make them like that?
You could map the most inner values or the mapped valued from the recursive call and assign the value to an object by respecting the index.
function map(object) {
return Object
.entries(object)
.reduce((r, [k, v]) => {
(Array.isArray(v) ? v : map(v)).forEach((w, i) => {
r[i] = r[i] || {};
r[i][k] = w;
});
return r;
}, []);
}
var data = { first: { arg: ["val1", "val2"] }, second: { arg1: ["val11", "val12"], arg2: ["val21", "val22"] } },
result = map(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Categories

Resources