How to Transpose a JavaScript Object into a Key/Value Array

How to transpose a javascript object into a key/value array

var data = { firstName: 'John', lastName: 'Doe', email: 'john.doe@gmail.com' }var output = Object.entries(data).map(([key, value]) => ({key,value}));
console.log(output);

Transpose an array of objects in JavaScript

Your desired result has duplicate properties, which is not allowed.

Instead, you could turn that inner structure into an array with just the values, and assign that to the property. Like this:

{
"shift": ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"],
"date": ["01/01/2016/08/00/00", "01/01/2016/17/00/00", // ...etc
}

You can use this ES6 code for that transformation:

const result = Object.assign(...Object.keys(myData[0]).map( key =>
({ [key]: myData.map( o => o[key] ) })
));

var myData = [ {"shift":"1","date":"01/01/2016/08/00/00","car":"178","truck":"255","bike":"317","moto":"237"},{"shift":"2","date":"01/01/2016/17/00/00","car":"125","truck":"189","bike":"445","moto":"273"},{"shift":"3","date":"02/01/2016/08/00/00","car":"140","truck":"219","bike":"328","moto":"412"},{"shift":"4","date":"02/01/2016/17/00/00","car":"222","truck":"290","bike":"432","moto":"378"},{"shift":"5","date":"03/01/2016/08/00/00","car":"200","truck":"250","bike":"420","moto":"319"},{"shift":"6","date":"03/01/2016/17/00/00","car":"230","truck":"220","bike":"310","moto":"413"},{"shift":"7","date":"04/01/2016/08/00/00","car":"155","truck":"177","bike":"377","moto":"180"},{"shift":"8","date":"04/01/2016/17/00/00","car":"179","truck":"203","bike":"405","moto":"222"},{"shift":"9","date":"05/01/2016/08/00/00","car":"208","truck":"185","bike":"360","moto":"195"},{"shift":"10","date":"05/01/2016/17/00/00","car":"150","truck":"290","bike":"315","moto":"280"},{"shift":"11","date":"06/01/2016/08/00/00","car":"200","truck":"220","bike":"350","moto":"205"},{"shift":"12","date":"06/01/2016/17/00/00","car":"230","truck":"170","bike":"390","moto":"400"}];
const result = Object.assign(...Object.keys(myData[0]).map( key => ({ [key]: myData.map( o => o[key] ) })));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to convert an object of key values to an array of objects

Try with array#map and Array#push

var obj={"name1":"value1","name2":"value2"};var res=[];Object.keys(obj).map(a => res.push({name:a , value:obj[a]}))console.log(res)

How to transpose array of object in javascript

You could transpose by getting the values and skipping the first property of the inner objects.

var data = { 0: { 0: "17", 1: 0.052708692712917476, 2: 0.05170448850051073, 3: 0.036428533456315845 }, 1: { 0: "16", 1: 0.039474102915939856, 2: 0.04788765943666215, 3: 0.03651675504080556 } },    result = Object.values(data).reduce(        (r, a, i) => (Object.values(a).slice(1).forEach((v, j) => (r[j] = r[j] || {})[i] = v), r),        {}    );
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to convert an array with one object and multiple keys into an array of multiple objects using those keys and their values?

You were pretty close. All you needed to do is specify the name:

const data = {
"category": "None",
"ARFE": 553.5,
"BV": 900,
"RF rfeer": 0
};

const result = Object
.entries(data)
.filter(([_, value]) => typeof value === 'number')
.map(([key, value]) => ({ name: key, value }));

console.log(result);

How to make array of objects with specific key and value from object?

const convertedArray = Object.keys(initObject).map((x)=> { return {
value : x,
label: initObject[x]
}});

How to convert Key Value list into array of Objects?

You can use Object.entries method to get a key-value pair array and Array#map method to iterate and create a customized array.

let obj = {    apple: "Apple",    banana: "Banana"};
let res =Object.entries(obj).map(([key, value]) => ({ key, value }))
console.log(res)

convert json response to array of keys and values javascript

map over the Object.entries and return a new array of objects.

const obj = {
"1":"a",
"2":"b"
}

const out = Object.entries(obj).map(([text, value]) => {
return { text, value };
});

console.log(out);

How to convert an object of just array values into an array of objects, each with transposed property values?

Used techniques of the below provided step by step example code ...

  • Object.assign

  • Object.values

  • Array.prototype.map

  • Array.prototype.reduce

// reducer function which transposes
// a table into a matrix and vice versa.
function transpose(result, row) {
return row.reduce((matrix, value, idx) => {

(matrix[idx] ??= []).push(value);
return matrix;

}, result);
}

const inputdata = {
data1: [5.0, 10.0, 50.0, 100.0, 500.0],
data2: [5.0, 10.0, 50.0, 100.0, 500.0],
data3: [5.0, 10.0, 50.0, 100.0, 500.0]
};
console.log(
Object

// convert `inputdata` into a table
// ... an array of row like arrays.
.values(inputdata)
);
console.log(
Object

// - convert `inputdata` into a table
// ... an array of row like arrays.
.values(inputdata)

// - then transpose the table into a matrix.
.reduce(transpose, [])
);
console.log(
Object

// - convert `inputdata` into a table
// ... an array of row like arrays.
.values(inputdata)

// - then transpose the table into a matrix.
.reduce(transpose, [])

// - then convert/map matrix
// into an array of objects.
.map(vector =>
vector.reduce((object, value, idx) =>

Object.assign(object, {
[`data${ idx + 1 }`]: value,
}), {}
)
)
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

Convert object's properties and values to array of key value pairs

You're probably looking for something along the lines of

var obj = {value1: 'prop1', value2: 'prop2', value3: 'prop3'};
var arr = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
arr.push(key + '=' + obj[key]);
}
};
var result = arr.join(',');
alert(result);

Notice that it will work fine if your values are strings; if they're complex objects then you'll need to add more code.

Or you can just use jQuery.param, which does what you want, even for complex types (although it uses the & character as the separator, instead of the comma.



Related Topics



Leave a reply



Submit