rank while looping through an array javascript - javascript

var testArray = [{
value: john,
count: 5
},
{
value: henry,
count: 2
},
{
value: bill,
count: 10
}]
testArray.map(function(value) {
//otherfunctions
})
So i have a function where i am already mapping through an array of objects similar to the above. I want to add a third value to the objects of rank based upon the count.
My current thought is to finish my map that i am already doing and then re sort the data based upon the count and then assign a rank based upon the the sorted position in the array. But this seems to be long winded given i am already mapping the array?

Vanilla JS:
var testArray = [{
value: john,
count: 5
},
{
value: henry,
count: 2
},
{
value: bill,
count: 10
}]
let newArray = testArray.map(function(item) {
return {
value: item.value,
count: item.count,
newProperty: 'x'
}
}).sort(function(x, z) {
return x.count - z.count;
});
ES6:
let newArray = testArray
.map(item => {
return {
...item,
newProperty: 'x'
}
}).sort((x, z) => x.count - z.count);
P.S. this is the functional way of doing this computation, should have a o(n*nlog n) time, you could do it in a o(n) with an imperative approach, but this is easier to read/understand in my opinion.
EDIT 1
After comment from author: wants to add the current count to the items (cannot think of a case where this would be necessary) but just to indulge:
let newArray = testArray
.map((item, index) => {
return {
...item,
currentCount: index
}
}).sort((x, z) => x.count - z.count);
Read more about map

Related

How to retrieve the most recent object in an array prior to a given date?

I have a simple, unsorted array of objects:
const items = [
{ item: 'Two', date: '2018-01-01' },
{ item: 'Three', date: '2019-01-01' },
{ item: 'One', date: '2022-01-01' },
{ item: 'Four', date: '2021-01-01' },
];
I am trying to build a function that takes a single effDate parameter and returns the most recent item whose date property is prior to the effDate.
For example, if I pass 2020-08-22 as the parameter, the function would return item Three, since it is the closest to the effDate without going over.
I have tried to use a reducer, but I am sure I am doing this wrong, since it always returns the first object in the array:
const getItem = (effDate) => {
return items.reduce((prev, next) => {
return next.date > prev.date && next.date < effDate.date
? next
: prev;
});
};
So, without building an entire looping / comparison function myself, is this possible with the built-in JS tools?
As #Barmar said in comments:
Sort the array in reverse by date. Then use find() to find the first
element before effData.
sort date in descending order
find the nearest to the date effDate
const items = [{ item: 'Two', date: '2018-01-01' }, { item: 'Three', date: '2019-01-01' }, { item: 'One', date: '2022-01-01' }, { item: 'Four', date: '2021-01-01' }, ];
const getItem = (effDate) => {
//1.
let itemsSorted = items.slice().sort((a, b) => Date.parse(b.date) - Date.parse(a.date))
//2.
return itemsSorted.find(item => Date.parse(item.date) < Date.parse(effDate));
};
console.log(getItem('2020-08-22'));

How can I sort through an Axios response?

I am using Axios to execute a GET request to a public API, I need to combine the names if they are the same and add the values up to only show the top 20 (It's a large dataset) based on the highest to lowest amounts(ascending order).
Axios Response
[
{
name: "foo1",
value: "8123.30"
},
{
name: "foo1",
value: "2852.13"
},
{
name: "foo2",
value: "5132.23"
},
{
name: "foo1",
value: "1224.20"
},
{
name: "foo2",
value: "1285.23"
}
1200...
];
Expected Output
[
{ name: "foo1",
value: "12199.63" // from all combined "foo1" amounts in the dataset
},
{
name: "foo2",
value: "6417.46" // from all combined "foo2" amounts in the dataset
},
18..
]
I tried to do something like this....
const fetchData = () => {
return axios.get(url)
.then((response) => response.data)
};
function onlyWhatINeed() {
const newArr = []
return fetchData().then(data => {
const sortedData = data.sort((a, b) => parseFloat(a.value) - parseFloat(b.value));
// I need to loop through the dataset and add all the "values" up
// returning only the top 20 highest values in an array of those objects
newArr.push(sortedData)
})
}
But I am confused as to how to push this data to a new array of the sorted data (top 20 values in ascending order) and use this data in my web application. I am a bit new to creating REST APIs so if you could provide articles and/or resources so I can understand a little more that would be an awesome bonus!
You can combine the entries that share the same name using a map, then sort the map and keep the first twenty elements :
function onlyWhatINeed() {
const newArr = []
return fetchData().then(data => {
let map = new Map();
data.forEach(d => {
if(!map.has(d.name)) {
map.set(d.name, parseFloat(d.value));
} else {
map.set(d.name, map.get(d.name) + parseFloat(d.value));
}
})
return Array.from(map.entries()).sort((a, b) => a.value - b.value).slice(0, 20);
})
}
Since you're dealing with a large dataset, I recommend that you handle this server side instead of offloading the sorting to your clients.
async function fetchData(){
const { data } = await axios.get(url);
let newArr = []
data.forEach((e,i) => {
let index = newArr.findIndex(el => el.name === e.name);
if(index !== -1 ) newArr[index].value += parseFloat(e.value); //add to the value if an element is not unique
if(index === -1 ) newArr.push({...e, value: parseFloat(e.value)}); //push to the array if the element is unique and convert value to float
});
return newArr.sort((a,b) => a.value - b.value).slice(0,20);//returns an array of 20 elements after sorting
}
Please do more research on how to work with arrays and objects in general.
If you happen to already be using lodash, then here's a functional-style solution using lodash chaining. Probably not optimal performance, but could be useful for relatively small datasets.
const _ = require('lodash');
const data = [
{
name: "foo1",
value: "8123.30"
},
{
name: "foo1",
value: "2852.13"
},
{
name: "foo2",
value: "5132.23"
},
{
name: "foo1",
value: "1224.20"
},
{
name: "foo2",
value: "1285.23"
},
{
name: "foo3",
value: "1000.00"
},
{
name: "foo3",
value: "2000.00"
}
];
// 1. convert string values to floats
// 2. group by name
// 3. sum values by name
// 4. sort by descending value
// 5. take top 20
const output =
_(data)
.map(obj => ({
name: obj.name,
value: parseFloat(obj.value)
}))
.groupBy('name')
.map((objs, key) => ({
name: key,
value: _.sumBy(objs, 'value')
}))
.orderBy(['value'], 'desc')
.slice(0, 20)
.value();
console.log('output:', output);

Javascript - How to sum the values in such an array?

I have such an array:
let array = {
[1]: {
name: 'test 1',
count: 5
},
[2]: {
name: 'test 2',
count: 3
}
}
How can I sum the values in the "count" column? Examples from simple arrays do not work. I currently have such a loop. Can it be done somehow better?
let sum = 0
Object.entries(array).forEach(([key, val]) => {
sum += val.count
});
Use reduce
let array = { 1: { name: "test 1", count: 5, }, 2: { name: "test 2", count: 3, }, };
total = Object.values(array).reduce((t, { count }) => t + count, 0); //t accumulator accumulates the value from previous calculation
console.log(total);
if you want to use a forEach loop like in your method use Object.values() instead because you only need values to calculate the sum of count
let array = {
1: { name: "test 1", count: 5 },
2: { name: "test 2", count: 3 },
};
let sum = 0;
Object.values(array).forEach(({ count }) => {
sum += count;
});
console.log(sum);
Building on top of the answer provided by #Sven.hig
Since you are calling the object "array" you might want to use an actual array instead.
Creating some functions to abstract away the complexity will help you understand your code better, when you come back to it in the future.
const add = (a, b) => a + b;
const sum = arr => arr.reduce(add, 0);
const data = [{
name: "test 1",
count: 5,
}, {
name: "test 2",
count: 3,
}
];
const total = sum(
data.map(d => d.count)
);
console.log(total);

minimum and maximum values of keys - group

Group by same key, find min and max value. Keys are ordered.
[{key:1,val:2}
{key:1,val:3}
{key:1,val:4}
{key:2,val:4}
{key:2,val:21}
{key:2,val:22}]
to
[2,4,1,22]
Thing is, the keys are ordered. For a real time app, i have an array of 20000. I can loop each item and check its group and act but i feel like i should not loop every object. There might be other possible solutions like picking random index and relying on the order for optimized code.
Any efficent way is appriciated. I have come up with few solutions but i have a large dataset to pass to chart, i need to optimize.
You could take a hash table for the grouping and get the min and max value by using a default array and map the new minima and maxima.
var data = [{ key: 1, val: 2 }, { key: 1, val: 3 }, { key: 1, val: 4 }, { key: 2, val: 4 }, { key: 2, val: 21 },{ key: 2, val: 1 }],
result = Object
.values(data.reduce(
(r, { key, val }) =>
(t => {
r[key] = ['min', 'max'].map((m, i) => Math[m](t[i], val));
return r;
})
(r[key] || [+Infinity, -Infinity]),
{}
))
.reduce((a, b) => a.concat(b));
console.log(result);

How to combine _.map and _.filter in a more efficient way?

I am using Lodash in my Angular project and I was wondering if there is a better way to write the following code:
$scope.new_arr = _.map(arr1, function(item){
return _.assign(item, {new_id: _.find(arr2, {id: item.id})});
});
$scope.new_arr = _.filter($scope.new_arr, function (item) {
return item.new_id !== undefined;
});
I am trying to combine values from one array to same objects in other array, and I want to ignore the objects that not appear in both arrays (it is something like join or left outer join in the sql language).
Here is a fiddle with an example of this code: Click me!
i think is better to use chaining
$scope.new_arr = _.chain(arr1)
.map(function(item) {
return _.merge(
{}, // to avoid mutations
item,
{new_id: _.find(arr2, {id: item.id})}
);
})
.filter('new_id')
.value();
https://jsfiddle.net/3xjdqsjs/6/
try this:
$scope.getItemById = (array, id) => {
return array.find(item => item.id == id);
};
$scope.mergeArrays = () => {
let items_with_ids = arr1.filter(item => !_.isNil($scope.getItemById(arr2,item.id)));
return items_with_ids.map(item => _.assign(item, {new_id: $scope.getItemById(arr2,item.id)}));
};
The answers provided here are all runtime of O(n^2), because they first run an outer loop on the first array, with an inner loop on the second array. You can instead run this in O(n). First, create a hashmap of all the ids in arr2 in a single loop; this will allow us an order 1 lookup. In the second loop on arr1, check this hashmap to determine if those items exist with O(n). Total Complexity is n + n = 2n, which is just O(n).
// provision some test arrays
var arr1 = [
{
id: 2
},
{
id: 4
},
{
id: 6
}
]
var arr2 = [
{
id: 3
},
{
id: 4
},
{
id: 5
},
{
id: 6
}
]
// First, we create a map of the ids of arr2 with the items. Complexity: O(n)
var mapIdsToArr2Items = _.reduce(arr2, function(accumulator, item) {
accumulator[item.id] = item;
return accumulator;
}, {});
// Next, we use reduce (instead of a _.map followed by a _.filter for slightly more performance.
// This is because with reduce, we loop once, whereas with map and filter,
// we loop twice). Complexity: O(n)
var combinedArr = _.reduce(arr1, function(accumulator, item) {
// Complexity: O(1)
if (mapIdsToArr2Items[item.id]) {
// There's a match/intersection! Arr1's item matches an item in arr 2. Include it
accumulator.push(item);
}
return accumulator;
}, []);
console.log(combinedArr)
You could first make a Map with arr1 and then map the items of arr2 with the properties of arr1.
var arr1 = [{ id: 1, title: 'z' }, { id: 2, title: 'y' }, { id: 3, title: 'x' }, { id: 4, title: 'w' }, { id: 5, title: 'v' }],
arr2 = [{ id: 2, name: 'b' }, { id: 3, name: 'c' }, { id: 4, name: 'd' }, { id: 5, name: 'e' }],
map = new Map(arr1.map(a => [a.id, a])),
result = arr2.map(a => Object.assign({}, a, map.get(a.id)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Categories

Resources