Javascript JSON array sorting - javascript

I am trying to build a custom sort function for a Weight Loss Challenge application and when users tries to click on Week 1 to sort the below JSON format data, the result doesn't appear to be correct. Appreciate if you can guide with what I am missing here. I would be changing the week 1 to another week if the function works out.
var array=[
{
"user": "Noah",
"weeklyPercentageChange":["10.37","-3.75","-2.21","-1.85","-1.68","-1.21","-1.41","-1.17","-0.47","-1.64","-23.36" ]
},
{
"user": "Liam",
"weeklyPercentageChange":["-4.49","-2.61","-1.19","-0.36","-1.39","-1.29","-0.44","-0.22","-0.67","-0.33","-12.53" ]
},
{
"user": "Mason",
"weeklyPercentageChange":["3.22","-2.40","0.35","-1.80","-0.25","-0.66","-2.42","0.00","1.71","-0.52","-9.00" ]
},
{
"user": "Ethan",
"weeklyPercentageChange":["-1.31","0.00","-1.75","-0.78","0.61","-1.79","0.80","0.00","0.00","0.38","-3.75"]
},
{
"user": "Alexander",
"weeklyPercentageChange":["-1.26","0.00","-1.48","-1.50","-0.07","-1.66","-0.53","-0.20","0.00","-0.86","-7.42"]
},
{
"user": "Maria",
"weeklyPercentageChange":["-1.10","-1.90","-0.11","-0.63","2.52","-1.01","0.06","-0.06","-0.06","1.38","-0.94" ]
},
{
"user": "Paul",
"weeklyPercentageChange":["1.08","-2.33","-0.10","-1.17","1.83","-1.70","-1.13","-0.19","0.00","0.00","-5.84"]
},
{
"user": "Steven",
"weeklyPercentageChange":["-0.77","-2.93","0.19","-1.22","-0.48","-1.39","-1.27","0.00","-0.23","-0.91","-8.84" ]
},
{
"user": "Edward",
"weeklyPercentageChange":["-0.71","-1.73","1.46","-0.17","0.42","-1.23","-1.26","1.47","0.59","-2.81","-3.98"]
},
{
"user": "Benjamin",
"weeklyPercentageChange":["0.56","-0.28","0.80","-0.94","0.00","0.90","-0.05","1.17","-1.87","0.98","0.14"]
},
{
"user": "Zachary",
"weeklyPercentageChange":["-0.47","-1.29","0.48","-1.59","1.37","-2.65","-0.24","0.66","0.05","0.24","-3.41"]
},
{
"user": "Austin",
"weeklyPercentageChange":["-0.33","0.00","-3.05","-2.02","0.80","-1.48","1.58","-0.43","0.87","-1.90","-5.87"]
},
{
"user": "Jordan",
"weeklyPercentageChange":["0.06","-2.18","2.17","-0.56","0.84","-0.67","0.45","0.39","0.28","0.06","0.67"]
},
{
"user": "Gabriel",
"weeklyPercentageChange":["0.00","0.00","-0.12","-0.81","0.25","0.00","0.00","0.00","0.00","0.00","-0.68"]
},
{
"user": "Wayne",
"weeklyPercentageChange":["0.20","-3.45","0.35","0.49","0.56","0.00","-0.61","0.54","0.54","-0.41","-1.83"]
},
{
"user": "Willie",
"weeklyPercentageChange":["-0.83","-3.21","0.71","0.00","-1.05","0.35","0.07","0.14","-0.07","-1.45","-3.72"]
},
{
"user": "Frank",
"weeklyPercentageChange":["0.00","0.00","-3.04","0.88","1.37","-0.41","-1.21","2.10","0.00","-0.58","-0.94"]
}
];
function GetSortOrder(prop){
return function(a,b){
if( a[prop] > b[prop]){
return 1;
}else if( a[prop] < b[prop] ){
return -1;
}
return 0;
}
}
/* array.sort( GetSortOrder("user") );
document.write("Sorted User Names : <br>");
for (var item in array) {
document.write("<br>"+array[item].user);
} */
array.sort( GetSortOrder("weeklyPercentageChange[0]") );
document.write("<br><br> Sorted Weekly Percentage Change : <br>");
for (var item in array) {
document.write("<br>"+array[item].weeklyPercentageChange[0]);
}

GetSortOrder("weeklyPercentageChange[0]") will result in the literal property name "weeklyPercentageChange[0]" being looked up - that is, if the objects were defined something like
{
user: 'name',
'weeklyPercentageChange[0]': 55
}
This isn't what you have, which is why things aren't working as expected.
For a general solution of navigating a nested object or array to find a value to sort by, consider passing a callback instead, which navigates to the nested value you want to compare against; here, obj => obj.weeklyPercentageChange[0].
You can also make the code significantly more concise by simply returning the difference between the values:
var array=[{user:"Noah",weeklyPercentageChange:["10.37","-3.75","-2.21","-1.85","-1.68","-1.21","-1.41","-1.17","-0.47","-1.64","-23.36"]},{user:"Liam",weeklyPercentageChange:["-4.49","-2.61","-1.19","-0.36","-1.39","-1.29","-0.44","-0.22","-0.67","-0.33","-12.53"]},{user:"Mason",weeklyPercentageChange:["3.22","-2.40","0.35","-1.80","-0.25","-0.66","-2.42","0.00","1.71","-0.52","-9.00"]},{user:"Ethan",weeklyPercentageChange:["-1.31","0.00","-1.75","-0.78","0.61","-1.79","0.80","0.00","0.00","0.38","-3.75"]},{user:"Alexander",weeklyPercentageChange:["-1.26","0.00","-1.48","-1.50","-0.07","-1.66","-0.53","-0.20","0.00","-0.86","-7.42"]},{user:"Maria",weeklyPercentageChange:["-1.10","-1.90","-0.11","-0.63","2.52","-1.01","0.06","-0.06","-0.06","1.38","-0.94"]},{user:"Paul",weeklyPercentageChange:["1.08","-2.33","-0.10","-1.17","1.83","-1.70","-1.13","-0.19","0.00","0.00","-5.84"]},{user:"Steven",weeklyPercentageChange:["-0.77","-2.93","0.19","-1.22","-0.48","-1.39","-1.27","0.00","-0.23","-0.91","-8.84"]},{user:"Edward",weeklyPercentageChange:["-0.71","-1.73","1.46","-0.17","0.42","-1.23","-1.26","1.47","0.59","-2.81","-3.98"]},{user:"Benjamin",weeklyPercentageChange:["0.56","-0.28","0.80","-0.94","0.00","0.90","-0.05","1.17","-1.87","0.98","0.14"]},{user:"Zachary",weeklyPercentageChange:["-0.47","-1.29","0.48","-1.59","1.37","-2.65","-0.24","0.66","0.05","0.24","-3.41"]},{user:"Austin",weeklyPercentageChange:["-0.33","0.00","-3.05","-2.02","0.80","-1.48","1.58","-0.43","0.87","-1.90","-5.87"]},{user:"Jordan",weeklyPercentageChange:["0.06","-2.18","2.17","-0.56","0.84","-0.67","0.45","0.39","0.28","0.06","0.67"]},{user:"Gabriel",weeklyPercentageChange:["0.00","0.00","-0.12","-0.81","0.25","0.00","0.00","0.00","0.00","0.00","-0.68"]},{user:"Wayne",weeklyPercentageChange:["0.20","-3.45","0.35","0.49","0.56","0.00","-0.61","0.54","0.54","-0.41","-1.83"]},{user:"Willie",weeklyPercentageChange:["-0.83","-3.21","0.71","0.00","-1.05","0.35","0.07","0.14","-0.07","-1.45","-3.72"]},{user:"Frank",weeklyPercentageChange:["0.00","0.00","-3.04","0.88","1.37","-0.41","-1.21","2.10","0.00","-0.58","-0.94"]}];
const GetSortOrder = getProp => (a,b) => getProp(a) - getProp(b);
array.sort( GetSortOrder(obj => obj.weeklyPercentageChange[0]) );
document.write("<br><br> Sorted Weekly Percentage Change : <br>");
for (var item in array) {
document.write("<br>"+array[item].weeklyPercentageChange[0]);
}

Related

Return additional properties using reduce and spread operator JS?

I am using the reduce function below to count how many times a players name is mentioned and then list them based on who was mentioned the most to the least.
I am trying to return the 2nd property [`${value.subtitles[0].name} + ${index}`] : value.subtitles[0].url with my object and sort it. However it is not sorting properly. When only returning the first property [value.title]: (acc[value.title] || 0) + 1, everything works as intended. But the second property is making it sort incorrectly. It is supposed to be sorting based on the title property value which is an integer of how many times that player was mentioned, from most to least. Why is this happening?
Thanks for the help!
const players = [
{
"title": "Mike",
"titleUrl": "https://mikegameplay",
"subtitles": [
{
"name": "Mike Channel",
"url": "https://channel/mike"
}
]
},
{
"title": "Cindy",
"titleUrl": "https://cindy",
"subtitles": [
{
"name": "Cindy Channel",
"url": "https://channel/cindy"
}
]
},
{
"title": "Mike",
"titleUrl": "https://mike",
"subtitles": [
{
"name": "Mike Channel",
"url": "https://channel/mike"
}
]
},
{
"title": "Haley",
"titleUrl": "https://Haley",
"subtitles": [
{
"name": "Haley Channel",
"url": "https://channel/haley"
}
]
},
{
"title": "Haley",
"titleUrl": "https://Haley",
"subtitles": [
{
"name": "Haley Channel",
"url": "https://channel/haley"
}
]
},
{
"title": "Haley",
"titleUrl": "https://Haley",
"subtitles": [
{
"name": "Haley Channel",
"url": "https://channel/haley"
}
]
}
]
const counts = players.reduce((acc, value, index) => ({
...acc,
[value.title]: (acc[value.title] || 0) + 1,
[`${value.subtitles[0].name} + ${index}`] : value.subtitles[0].url
}), {});
const sortedValues = [];
for (const value in counts) {
sortedValues.push([value, counts[value]]);
};
sortedValues.sort((a, b) => b[1] - a[1]);
console.log(sortedValues)
try this
var groupBy = function (xs, key) {
return xs.reduce(function (rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
var pl = groupBy(players, "title");
console.log(pl);
let sortable = [];
for (var item in pl) {
sortable.push([item, pl[item].length, pl[item][0].subtitles[0].url]);
}
sortable.sort(function (a, b) {
return b[1] - a[1];
});
console.log(JSON.stringify(sortable));
result
[["Haley",3,"https://channel/haley"],["Mike",2,"https://channel/mike"],["Cindy",1,"https://channel/cindy"]]

Loop through JSON array of objects and get the properties based on the matching IDs from objects

My target is if the id from digital_assets and products matches then get the value of URL fro digital_assets and ProductName from products object. I'm able to traverse through the object and get the values of digital_assets and products but need some help to compare these two objects based on IDs to get the value of URL and ProductName. Below is what I've done so far.
var data = [{
"digital_assets": [{
"id": "AA001",
"url": "https://via.placeholder.com/150"
},{
"id": "AA002",
"url": "https://via.placeholder.com/150"
}]
}, {
"products": [{
"id": ["BB001", "AA001"],
"ProductName": "PROD 485"
},{
"id": ["BB002", "AA002"],
"ProductName": "PROD 555"
}]
}
];
$.each(data, function () {
var data = this;
//console.log(data);
$.each(data.digital_assets, function () {
var dAssets = this,
id = dAssets['id'];
// console.log(id);
});
$.each(data.products, function () {
var proData = this,
prod_id = proData['id'];
// console.log(prod_id);
$.each(prod_id, function () {
var arr_id = this;
console.log(arr_id);
});
});
});
Do I need to create new arrays and push the values into the new arrays? Then concat() these array to one. ? Bit lost any help will be appreciated.
Here is one way you can do this via Array.reduce, Array.includes, Object.entries and Array.forEach:
var data = [{ "digital_assets": [{ "id": "AA001", "url": "https://via.placeholder.com/150" }, { "id": "AA002", "url": "https://via.placeholder.com/150" } ] }, { "products": [{ "id": ["BB001", "AA001"], "ProductName": "PROD 485" }, { "id": ["BB002", "AA002"], "ProductName": "PROD 555" } ] } ]
const result = data.reduce((r,c) => {
Object.entries(c).forEach(([k,v]) =>
k == 'digital_assets'
? v.forEach(({id, url}) => r[id] = ({ id, url }))
: v.forEach(x => Object.keys(r).forEach(k => x.id.includes(k)
? r[k].ProductName = x.ProductName
: null))
)
return r
}, {})
console.log(Object.values(result))
You can use Array.prototype.find, Array.prototype.includes and Array.prototype.map to achieve this very gracefully.
let data = [
{
"digital_assets": [
{
"id": "AA001",
"url": "https://via.placeholder.com/150"
},
{
"id": "AA002",
"url": "https://via.placeholder.com/150"
}
]
},
{
"products": [
{
"id": ["BB001", "AA001"],
"ProductName": "PROD 485"
},
{
"id": ["BB002","AA002"],
"ProductName": "PROD 555"
}
]
}
];
// Find the 'digital_assets' array
let assets = data.find(d => d['digital_assets'])['digital_assets'];
// Find the 'products' array
let products = data.find(d => d['products'])['products'];
// Return an array of composed asset objects
let details = assets.map(a => {
return {
id : a.id,
url : a.url
name : products.find(p => p.id.includes(a.id)).ProductName
};
});
console.log(details);
changed answer to fit your needs:
var data = [
{
"digital_assets": [
{
"id": "AA001",
"url": "https://via.placeholder.com/150"
},
{
"id": "AA002",
"url": "https://via.placeholder.com/150"
}
]
},
{
"products": [
{
"id": ["BB001", "AA001"],
"ProductName": "PROD 485"
},
{
"id": ["BB002","AA002"],
"ProductName": "PROD 555"
}
]
}
]
let matchingIds = [];
let data_assetsObject = data.find(element => {
return Object.keys(element).includes("digital_assets")
})
let productsObject = data.find(element => {
return Object.keys(element).includes("products")
})
data_assetsObject["digital_assets"].forEach(da => {
productsObject["products"].forEach(product => {
if (product.id.includes(da.id)){
matchingIds.push({
url: da.url,
productName: product.ProductName
})
}
})
})
console.log(matchingIds);
working fiddle: https://jsfiddle.net/z2ak1fvs/3/
Hope that helped. If you dont want to use a new array, you could also store the respective data within the element you are looping through.
Edit:
I think i know why i got downvoted. My example works by making data an object, not an array. changed the snippet to show this more clearly.
Why is data an array anyway? Is there any reason for this or can you just transform it to an object?
Edit nr2:
changed the code to meet the expectations, as i understood them according to your comments. it now uses your data structure and no matter whats in data, you can now search for the objects containing the digital_assets / products property.
cheers
https://jsfiddle.net/2b1zutvx/
using map.
var myobj = data[0].digital_assets.map(function(x) {
return {
id: x.id,
url: x.url,
ProductName: data[1].products.filter(f => f.id.indexOf(x.id) > -1).map(m => m.ProductName)
};
});

cannot update an array of elements via a 2d iteration

I have two arrays of object, the first array (printerChart, around 80 elements) is made of the following type of objects:
[{
printerBrand: 'Mutoh',
printerModel: 'VJ 1204G',
headsBrand: 'Epson',
headType: '',
compatibilty: [
'EDX',
'DT8',
'DT8-Pro',
'ECH',
],
},
....
]
The second array (items, around 500 elements) is made of the following type of objects:
[
{
"customData": {
"brand": {
"value": {
"type": "string",
"content": "hp"
},
"key": "brand"
},
"printer": {
"value": {
"type": "string",
"content": "c4280"
},
"key": "printer"
}
},
"name": "DT8 XLXL",
"image": {
"id": "zLaDHrgbarhFSnXAK",
"url": "https://xxxxxxx.net/images/xxxxxx.jpg"
},
"brandId": "xxxxx",
"companyId": "xxxx",
"createdAt": "2018-03-26T14:39:47.326Z",
"updatedAt": "2018-04-09T14:31:38.169Z",
"points": 60,
"id": "dq2Zezwm4nHr8FhEN"
},
...
]
What I want to do is to iterate via the second array and, if the part of the name of an item (i.e. DT8) is included in an element of the array 'compatibility' of the first array, I would like to include a new properties to it from the element of the first array: printerBrand. I have tried but somehow the iteration doesn't take place correctly. This is what I tried:
items.forEach((item) => {
printerChart.forEach((printer) => {
if (printer.compatibilty.some(compatibleElem => (
item.name.includes(compatibleElem)))) {
item.printerBrand = printer.printerBrand;
} else {
item.printerBrand = '';
}
});
});
What am I doing wrong?
You do
items.items.forEach(...)
Shouldn't you be doing
items.forEach(...)
?
I suggest to initialize item.printerBrand with an empty string and use a nested approach of some for getting a brand and to exit the loops, if found.
This prevents to get an empty string even if there is a brand to assign.
items.forEach((item) => {
item.printerBrand = '';
printerChart.some(printer => {
if (printer.compatibilty.some(compatibleElem => item.name.includes(compatibleElem))) {
item.printerBrand = printer.printerBrand;
return true;
}
});
});

How to append object-key value form one array to other array?

I have an existing array with multiple object. With an interval I would like to update the existing array with values from another array. See the (simplified) example below.
I've serverall gools:
Copy the value of fan_count form the new array, to the current array with the key "fan_count_new"
If a object is removed or added in the New array, it have to do the same to the Current array.
As far I can see now, I can use some es6 functions :) like:
object-assign, but how to set the new key "fan_count_new"?
How to loop through the array to compare and add or remove + copy the fan_count?
Current array:
[{
"fan_count": 1234,
"id": "1234567890",
"picture": {
"data": {
"url": "https://scontent.xx.fbcdn.net/v/photo.png"
}
}
},
{
"fan_count": 4321,
"id": "09876543210",
"picture": {
"data": {
"url": "https://scontent.xx.fbcdn.net/v/photo.png"
}
}
}, ...
]
New array:
[{
"fan_count": 1239,
"picture": {
"data": {
"url": "https://scontent.xx.fbcdn.net/v/photo.png"
}
"id": "1234567890"
},
{
"fan_count": 4329,
"picture": {
"data": {
"url": "https://scontent.xx.fbcdn.net/v/photo.png"
}
},
"id": "09876543210"
}, ...
]]
You can remove elements which doesn't exists in new array by using array.filter and you can loop through the new array to update the same object in the current array:
var currArr = [
{
"fan_count": 1234,
"id": "1234567890",
},
{
"fan_count": 4321,
"id": "09876543210",
},
{
"fan_count": 4321,
"id": "09876543215",
}
];
var newArr = [
{
"fan_count": 1234,
"id": "1234567890"
},
{
"fan_count": 5555,
"id": "09876543210"
}
];
currArr = currArr.filter(obj => newArr.some(el => el.id === obj.id));
newArr.forEach(obj => {
var found = currArr.find(o => o.id === obj.id);
if (found) {
found.fan_count_new = obj.fan_count;
}
});
console.log(currArr);
Later on I realised that is was better to turn it around, add the fan_count form the currArr to the new one. This because it is easier to handle new objects, and you dont't have to deal with deleted objects. So, anybody how is looking for something like this:
newArr.forEach(obj => {
var found = currArr.find(o => o.id === obj.id);
if (found) {
console.log('found: ', found.fan_count, obj.fan_count)
obj.fan_count_prev = found.fan_count;
obj.fan_count_diff = Math.round(obj.fan_count - found.fan_count);
}
if (typeof obj.fan_count_prev === "undefined") {
obj.fan_count_prev = obj.fan_count;
obj.fan_count_diff = 0
}
});

Group and count values in an array

I have an array with objects, like the following.
b = {
"issues": [{
"fields": {
"status": {
"id": "200",
"name": "Backlog"
}
}
}, {
"fields": {
"status": {
"id": "202",
"name": "close"
}
}
}, {
"fields": {
"status": {
"id": "201",
"name": "close"
}
}
}]
};
I want to count how many issues have status close, and how many have backlog. I'd like to save the count in a new array as follows.
a = [
{Name: 'Backlog', count: 1},
{Name: 'close', count: 2}
];
I have tried the following.
b.issues.forEach(function(i) {
var statusName = i.fields.status.name;
if (statusName in a.Name) {
a.count = +1;
} else {
a.push({
Name: statusName,
count: 1
});
}
});
That however doesn't seem to be working. How should I implement this?
This is a perfect opportunity to use Array#reduce. That function will take a function that is applied to all elements of the array in order and can be used to accumulate a value. We can use it to accumulate an object with the various counts in it.
To make things easy, we track the counts in an object as simply {name: count, otherName: otherCount}. For every element, we check if we already have an entry for name. If not, create one with count 0. Otherwise, increment the count. After the reduce, we can map the array of keys, stored as keys of the object, to be in the format described in the question. See below.
var b = {
"issues": [{
"fields": {
"status": {
"id": "200",
"name": "Backlog"
}
}
}, {
"fields": {
"status": {
"id": "202",
"name": "close"
}
}
}, {
"fields": {
"status": {
"id": "201",
"name": "close"
}
}
}]
};
var counts = b.issues.reduce((p, c) => {
var name = c.fields.status.name;
if (!p.hasOwnProperty(name)) {
p[name] = 0;
}
p[name]++;
return p;
}, {});
console.log(counts);
var countsExtended = Object.keys(counts).map(k => {
return {name: k, count: counts[k]}; });
console.log(countsExtended);
.as-console-wrapper {
max-height: 100% !important;
}
Notes.
Array#reduce does not modify the original array.
You can easily modify the function passed to reduce to for example not distinguish between Backlog and backlog by changing
var name = c.fields.status.name;
into
var name = c.fields.status.name.toLowerCase();
for example. More advanced functionality can also easily be implemented.
Using ES6 Arrow functions you can do it with minimum syntax
var b = {
"issues": [{
"fields": {
"status": {
"id": "200",
"name": "Backlog"
}
}
}, {
"fields": {
"status": {
"id": "202",
"name": "close"
}
}
}, {
"fields": {
"status": {
"id": "201",
"name": "close"
}
}
}]
};
var countOfBackLog = b.issues.filter(x => {
return x.fields.status.name === "Backlog"
}).length
var countOfClose = b.issues.filter(x => {
return x.fields.status.name === "close"
}).length
a =[{Name: 'Backlog', count : countOfBackLog}, {Name: 'close', count : countOfClose}]
More about arrow functions here
You can write like this. It is dynamic.
var a = {};
for(var key in b["issues"]){
if(!a.hasOwnProperty(b["issues"][key].fields.status.name)){
a[b["issues"][key].fields.status.name] = 1;
}else{
a[b["issues"][key].fields.status.name] = a[b["issues"][key].fields.status.name]+1;
}
}
var c = [];
for(var key1 in a){
c.push({
name : key1,
count : a[key1]
});
}
Something like this should do the trick. Simply iterate over your data, keep 2 counters with the number of each type of issue, and create the data format you want in the end. Try it live on jsfiddle.
var b = {
"issues": [{
"fields": {
"status": {
"id": "200",
"name": "Backlog"
}
}
}, {
"fields": {
"status": {
"id": "202",
"name": "close"
}
}
}, {
"fields": {
"status": {
"id": "201",
"name": "close"
}
}
}]
};
var data = [];
for(var issue of b.issues){
var entryFound = false;
var tempObj = {
name: issue.fields.status.name,
count: 1
};
for(var item of data){
if(item.name === tempObj.name){
item.count++;
entryFound = true;
break;
}
}
if(!entryFound){
data.push(tempObj);
}
}
console.log(data);

Categories

Resources