JSON object string to convert an array using map [duplicate] - javascript

This question already has answers here:
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
Closed 3 years ago.
Here is my response from my server.
var my_array = [{"1":"1"},{"2":"8"},{"4":"13"},{"5":"19"},{"6":"22"}]; //from server
I want to convert it like
var new_array = { 1 : "1", 2 : "8", 4 : "13", 5 : "19" , 6 : "22"}
But how to convert it using map function
new_array = my_array.map(function (a) {
return ???
});

Use reduce with spreading - you can't map an array to an object. Spreading also allows for multiple properties in each object.
var new_array = my_array.reduce((a, c) => ({ ...a, ...c }), {});
You could also use Object.fromEntries after flatMapping the entries:
var new_array = Object.fromEntries(my_array.flatMap(Object.entries));

Related

How can I get the index of a property inside a JS object using the property key not the value using Javascript? [duplicate]

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 1 year ago.
I am having a simple sample code as follows:
var sampleObj = [{prop1 : ""}, {prop2 : ""}, {propArr : [{key : "", value : ""}, {key : "", value : ""}]}];
This is a JSON Object that has some properties including a property that contains an array.
My Question
I need to use the JS method indexOf() to get the index of [propArr] using the key not the value.
for example
I need a way to implement this:
var index = sampleObj.indexOf(propArr);
Use findIndex:
var sampleObj=[{prop1:""},{prop2:""},{propArr:[{key:"",value:""},{key:"",value:""}]}];
const prop = "propArr"
const res = sampleObj.findIndex(e => Object.keys(e)[0] == prop)
console.log(res)
If you strictly need to use indexOf, you can map through the array to extract the first property of each object, then use indexOf:
var sampleObj=[{prop1:""},{prop2:""},{propArr:[{key:"",value:""},{key:"",value:""}]}];
const prop = "propArr"
const res = sampleObj.map(e => Object.keys(e)[0]).indexOf(prop)
console.log(res)

javascript: create dictionary from array of key-values objects [duplicate]

This question already has answers here:
How to convert an array of objects to object with key value pairs
(6 answers)
Merge multiple objects inside the same array into one object [duplicate]
(2 answers)
How to concatenate properties from multiple JavaScript objects
(14 answers)
Closed 1 year ago.
I have an array of objects.
const ar = [ {0 : "A"}, {1 : "B"}];
I want to create an dictionary from it
const di = {0 : "A", 1 : "B"};
I am a little struggling with this conversion. How it is possible to do it using map or reduce?
const di = ar.map( ob => (???) }
A simple reduce can be used to merge all objects. Be aware that the keys need to be unique otherwise you will overwrite them.
The merging is done with the spread syntax
const ar = [{0 : "A"}, {1 : "B"}];
const di = ar.reduce((acc, obj) => ({...acc, ...obj}));
console.log(di);
Another simple approach is the use of Object.assign
const ar = [{0 : "A"}, {1 : "B"}];
const di = Object.assign({}, ...ar);
console.log(di);
You should use reduce here instead of map. map will give you an array of objects but what you need is an object will all keys. So reduce is the best tool for it.
Main difference between map and reduce
const ar = [{ 0: "A" }, { 1: "B" }];
const di = { 0: "A", 1: "B" };
const result = ar.reduce((acc, curr) => {
Object.keys(curr).forEach((k) => (acc[k] = curr[k]));
return acc;
}, {});
console.log(result);

Turn array with two elements into JS object with one k/v-pair [duplicate]

This question already has answers here:
Creating object with dynamic keys [duplicate]
(2 answers)
Closed 2 years ago.
I have this array: ["description", "asc"]
and want to turn it into this object:
{ "description": "asc" }
What's the fastest way to achieve this?
I tried
const obj = { array[0] : array[1] }
But it doesn't work because I can't seem to set the key of the object dynamically?
you can try this
const array = new Map([
["description", "asc"]
]);
const convobj = Object.fromEntries(array);
console.log(convobj)
You can do this:
let array = ["description", "asc"];
const obj = { [array[0]]: array[1] };
console.log(obj);
You can try this:
const bar = {
[array[0]]: array[1]
}

Javascript to Group Array of Object into combination of two values [duplicate]

This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Create Multilevel groups from an Array of Objects
(2 answers)
Closed 4 years ago.
var data=[
{custType:1,code:'savings',efDate:'2/3/19',exDate'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'current',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'current',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}
];
expected results
var modifiedData=[
[
savings=[ {custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:1,code:'savings',efDate:'2/3/19',exDate:'2/3/19'}
],
current=[ {custType:1,code:'current',efDate:'2/3/19',exDate:'2/3/19'} ]
],
[
savings=[ {custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'},
{custType:2,code:'savings',efDate:'2/3/19',exDate:'2/3/19'} ],
current=[ {custType:2,code:'current',efDate:'2/3/19',exDate:'2/3/19'} ]
]
];
as i am trying to find a group with (custType and code), i am not able to generate the expected result. Which method i should use here? can i use reduce or groupBy. I am confused about it.
You could build up a nested hashtable:
const hash = {};
for(const value of data) {
const group = hash[value.custType] || (hash[value.custType] = {});
const group2 = group[value.code] || (group[value.code] = []);
group2.push(value);
}
const result = Object.values(hash).map(Object.values);

Finding values of a mapping/dict [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 9 years ago.
I have map/dictionary in Javascript:
var m = {
dog: "Pluto",
duck: "Donald"
};
I know how to get the keys with Object.keys(m), but how to get the values of the Object?
You just iterate over the keys and retrieve each value:
var values = [];
for (var key in m) {
values.push(m[key]);
}
// values == ["Pluto", "Donald"]
There is no similar function for that but you can use:
var v = Object.keys(m).map(function(key){
return m[key];
});

Categories

Resources