This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 5 years ago.
How covert or add array elements into object.
Now
arr = [
["1/1/2017",113],
["3/11/2017",58],
["3/12/2017",70],
["3/13/2017",76]
]
Should be
arr = [
{date:"1/1/2017", count:113},
{date:"3/11/2017", count:58},
{date:"3/12/2017", count:70},
{date:"3/13/2017", count:76}
]
arr.map(function(arr1) {
return {
date : arr1[0],
count : arr1[1]
}
})
Related
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
How to sort strings in JavaScript
(16 answers)
Sort Array by ISO 8601 date
(8 answers)
Closed last year.
I have the following nested object. I would like to iterate through every item (latest.sentTime) and then sort the object itself by "sentTime". So that the most recent message is on top. How can I achieve this?
In the following example, "0" and "1" need to be swapped basically, since "1" has a more recent "sentTime".
Using Array#map, iterate over the array to get latest.sentTime
Using Array#sort and Date, sort the items
const arr = [
{ latest: { sentTime: '2022-02-05T19:15:32.000Z' } },
{ latest: { sentTime: '2022-02-06T22:12:00.000Z' } }
];
const sentTimes = arr
.map(({ latest }) => latest.sentTime)
.sort((a, b) => new Date(b) - new Date(a));
console.log(sentTimes);
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);
This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 6 years ago.
I want to sort the array of array on length of string, as follows
var arr = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
var sortedArr = sortOnLengthOfString(arr);
>> sortedArr = [
[1951, "Mayfer"],
[1101, "Cetaphil"],
[1007, "Ferri Injection"],
[1785, "Actinac Capsules"]
]
Is there a solution with lodash preferably? Or plain Javascript?
I haven't found duplicate question yet. Can someone find it? Please note, the sorting I am asking for, is for array of arrays and not array of objects.
You can use Array#sort at this context,
var obj = [
[1951, "Mayfer"],
[1785, "Actinac Capsules"],
[1007, "Ferri Injection"],
[1101, "Cetaphil"]
];
obj.sort(function(a,b){
return a[1].length - b[1].length
});
console.log(obj);
//[[1951, "Mayfer"],[1101, "Cetaphil"],[1007, "Ferri Injection"],[1785, "Actinac Capsules"]]
This question already has answers here:
Obtain smallest value from array in Javascript?
(18 answers)
Closed 7 years ago.
Suppose i have following Json object.
{
"orderId": ["80055517"],
"orderItemId": [
"850057658",
"850057657",
"850057656"
]
}
Now i want the least value i.e 850057656 from orderitemId.
Assuming you do not have orderItemId sorted. You can get the result using following.
var obj = {
"orderId": ["80055517"],
"orderItemId": [
"850057658",
"850057657",
"850057656"
]
};
console.log(obj.orderItemId.sort()[0]);
var a = {
"orderId": ["80055517"],
"orderItemId": [
"850057658",
"850057657",
"850057656"
]
};
alert(a.orderItemId[a.orderItemId.length-1]);
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];
});