Find closest object values inside object of objects - javascript

Let's start with an example.
I have a list of fruits and it's nutritions stored in JS objects. Then I get a vegetable which is not in the list but has same types of values as the keys in the fruits object.
How can I get the closest fruit from the fruit object to the vegetable given vegetable if 1) the nutrition values (sugar,salt,...) has same values (sugar = salt) 2) the nutrition values (sugar,salt,...) have different values (sugar > salt, so basicly check only for sugar). I know that's not a good explanation, but let's check the example below.
let fruits = {
apple:{
sugar:12,
salt:5
},
banana:{
sugar:13,
salt:3
},
}
let cucumber = {
sugar:12,
salt:3
}
let closestFruitToCucumber = closest(cucumber, fruits)
// apple (if checking only sugar)
// banana (if both sugar and salt are valued the same)
function closest(vegetable,fruitList){
// here is the part which I am looking for.
}
I can post the code, which I have tried. There were plenty of them but none of them worked at all.

Following up on my comment:
I guess that you want to calculate the absolute difference for every matching key in the objects, and then sum these differences for each comparison, finally selecting the comparison with the lowest sum.
I'd use a functional approach so that you can refine your result by a query parameter (property key(s)). This approach will continue to work even if the two objects don't share the same property keys, and it will also allow you to easily refactor if the standard property keys change in your data:
const defaultKeys = ['salt', 'sugar'];
function getSimilarityScore (a, b, keys = defaultKeys) {
let score = 0;
for (const key of keys) score += Math.abs((a[key] ?? 0) - (b[key] ?? 0));
return score;
}
function closest (objList, obj, keys = defaultKeys) {
let minScore = Number.POSITIVE_INFINITY;
let result;
for (const o of Object.values(objList)) {
const score = getSimilarityScore(obj, o, keys);
if (score >= minScore) continue;
minScore = score;
result = o;
}
return result;
}
const fruits = {
apple: {
salt: 5,
sugar: 12,
},
banana: {
salt: 3,
sugar: 13,
},
};
const cucumber = {
salt: 3,
sugar: 12,
};
const closestToCucumber = closest(fruits, cucumber);
const closestToCucumberBySalt = closest(fruits, cucumber, ['salt']);
const closestToCucumberBySugar = closest(fruits, cucumber, ['sugar']);
console.log(closestToCucumber === fruits.banana); // true
console.log(closestToCucumberBySalt === fruits.banana); // true
console.log(closestToCucumberBySugar === fruits.apple); // true

You can implement a function for this purpose and compute the sum of absolute differences, finding the minimum such sum and returning the fruit corresponding this value. I returned its name, but if you want a different return format, let me know.
let fruits = {
apple:{
sugar:12,
salt:5
},
banana:{
sugar:13,
salt:3
},
}
let cucumber = {
sugar:12,
salt:3
}
function getClosestFruit(fruits, vegetable) {
let output = undefined;
let score = 0;
for (let key in fruits) {
let fruit = fruits[key];
let currentScore = Math.abs(fruit.sugar - vegetable.sugar) + Math.abs(fruit.salt - vegetable.salt);
if ((!output) || (score > currentScore)) {
output = key;
score = currentScore;
}
}
return output;
}
console.log(getClosestFruit(fruits, cucumber));

Related

I need help simplifying code that compares various properties of objects

I have a strong suspicion my code is way too clunky and can be written in a more concise and efficient manner. The intent is to compare an array of objects (representing players) to find the player with the highest majorityScore property. If there are more than one player sharing the same high score, then compare the co-winners' factions with a map (priorityMap) to determine who wins.
players = [
{
majorityScore: 4,
faction: 'AR'
},
{
majorityScore: 8,
faction: 'MOU'
},
{
majorityScore: 2,
faction: 'MOB'
},
{
majorityScore: 8,
faction: 'I'
}
];
const priorityMap = {
'MOB': 1,
'I': 2,
'MOU': 3,
'AR': 4,
'S' : 0
}
let winner;
let highScore = -1;
let duplicates = [];
for(let i = 0; i < players.length; i++){
if(players[i].majorityScore > highScore){
highScore = players[i].majorityScore;
winner = players[i]
duplicates = [winner];
} else if (players[i].majorityScore === highScore){
duplicates.push(players[i]);
};
}
if(duplicates.length > 1){
let highFactionScore = duplicates.reduce((a,v) => {
if(priorityMap[v.faction] > a){
a = priorityMap[v.faction];
}
return a;
}, 0);
let winningFaction = Object.keys(priorityMap).find((k) => {
return priorityMap[k] === highFactionScore;
});
winner = duplicates.filter((v) => {
return v.faction === winningFaction
})
}
Since you are reducing an array of objects into one object, the reduce function can be used here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
Instead of checking the factions after getting the players with the highest majority score, you can check the factions whenever you end up comparing players with the same majority score.
let winner = players.reduce((accum, currVal) => {
if (accum.majorityScore < currVal.majorityScore) {
return currVal;
} else if (accum.majorityScore > currVal.majorityScore) {
return accum;
} else {
return priorityMap[accum.faction] > priorityMap[currVal.faction] ? accum : currVal
}
});
Here's a slightly shorter method. It uses a temp object to maintain a dictionary where the scores are the keys. As we iterate we add the value of priorityMap[faction] to the score property, and change it only if the value is greater than the one stored.
const players=[{majorityScore:4,faction:"AR"},{majorityScore:8,faction:"MOU"},{majorityScore:8,faction:"MOU12"},{majorityScore:8,faction:"MOU20"},{majorityScore:2,faction:"MOB"},{majorityScore:2,faction:"MOB2"},{majorityScore:8,faction:"I"}],priorityMap={MOB:1,I:2,MOU:3,MOB2:4,AR:4,S:0,MOU12:21,MOU20:22};
function getGroups(arr) {
// Create the temp object
// We'll be using the scores as keys, and using
// the value in priorityMap as the value
// As we iterate we check the new priorityMap value
// against the old one
const temp = {};
// Iterate over the players array
return arr.reduce((acc, c) => {
const { majorityScore: score, faction } = c;
// If the accumulator object doesn't have a key that
// matches the score, add a new object
acc[score] = (acc[score] || { majorityScore: score, faction });
// If the new value of priorityMap[faction] is
// greater than the one stored in temp
// update the faction in the accumulator for that score
if (priorityMap[faction] > temp[score]) {
acc[score].faction = faction;
}
// Update the temp object with the
// priorityMap[faction] value
temp[score] = priorityMap[faction];
// Return the accumulator for the next iteration
return acc;
}, {});
}
console.log(getGroups(players));

Is there any way to search for something below a certain value inside a couple arrays?

I have the following JSON:
{
"hominis": [20000, "asd"],
"omint": [30000, "asd"]
}
and I would like to make a function that returns all array names (hominis, omint) whose arr[0] value is under my input value
e.g:
if (myInput <= arr[0]) {
return arrName
}
I would like to go through my arrays (hominis, omint) and return their names if the condition is matched. I believe it has something to do with for loops, but I couldn't do it.
I've started JS two weeks ago so I'm a newbie.
Thank you
You can iterate over the keys of an object like following snippet,
var a = {
"hominis": [20000, "asd"],
"omint": [30000, "asd"]
};
var minValue = 25000;
var keys = Object.keys(a); // Returns all keys in the object
for (let key of keys) { // Loop over all the keys
if (a[key][0] > minValue) { // Check if value at key matches your condition
console.log(key); // return/add to your new array/ or whatever to the matching key
}
}
You did not say how to show your output. I am assuming it will be array of names.
let data = {
hominis: [20000, "asd"],
omint: [30000, "asd"],
};
myInput = 20001;
let x = Object.entries(data).reduce((p, [x, y]) => {
if (myInput <= y[0]) p.push(x);
return p;
}, []);
console.log(x);
You can use "for in obj".
var obj1 = {
"hominis": [20000, "asd"],
"omint": [30000, "asd"]
}
function findMinorThan(obj, limit) {
let res = []
for (const k in obj) {
if (obj[k][0] < limit) {
res.push(k)
}
}
return res
}
console.log(findMinorThan(obj1, 25000));

How do I find all 2 pairs of integers that have the same product in JavaScript?

I need to write a program that, when given a list of integers, it finds all 2-pairs of integers that have the same product. i.e. a 2-pair is 2 distinct pairs of integers lets say [(a,b),(c,d)] where a*b = c*d but a ≠ b ≠ c ≠ d.
The range of integers should be from 1 to 1024. What I would like to implement is that when the web page is opened the user is prompted by a pop up in which he will enter the array of integers, i.e [1,2,3,7,8,9,6] etc for instance from the input [1,2,3,7,8,9,6] the output should be [(9,2),(3,6)] since both evaluate to 18.
The coding I did so far is very basic and can be seen below. What I've done so far is the pop-up box alert the input etc, but can't seem to understand how to make the program check for the pairs and give the sum. Thanks in advance to this community who's helping me out to better understand and learn javascript!
I've done my fair bit of research below, definitely different question than mine but have gone through them.
Find a pair of elements from an array whose sum equals a given number
https://www.w3resource.com/javascript-exercises/javascript-array-exercise-26.php
Code:
function evaluate() {
const input = prompt("Please enter the array of integers in the form: 1,2,3,1")
.split(',')
.map(item => item.trim());
function pairs(items) {
}
if (input == "" || input == null) {
document.writeln("Sorry, there is nothing that can be calculated.");
} else {
document.writeln("Your calculation is: ");
document.writeln(pairs(input) + " with a starting input string of: " + input);
}
}
evaluate()
You could iterate the array and a copy of the array beginning by the actual index plus one for getting the products. Store the result in an object with product as key.
Then get the keys (products) of the object, filter it to get only the results with two or more products.
var array = [1, 2, 3, 7, 8, 9, 6],
result = {},
pairs;
array.forEach(function (a, i) {
array.slice(i + 1).forEach(function (b) {
(result[a * b] = (result[a * b] || [])).push([a, b]);
});
});
pairs = Object
.keys(result)
.filter(function (k) { return result[k].length >= 2; })
.map(function(k) { return result[k]; });
console.log(pairs);
We could mutate the equation:
a * b = c * d | : b
a = c * d : b
So actually we just need to get all different combinations of three numbers (b, c, d) and check if the result (a) is also in the given range:
while(true){
// shuffle
const [b, c, d] = items;
const a = c * d / b;
if(items.includes(a + ""))
return true;
}
return false;
Now you only need to shuffle the array to go through all different combinations. You can find an algorithm here
Assuming that you are given an array such as [1,2,3,7,8,9,6] and a value 18 and you need to find pairs that multiply to 18 then, use the following approach
Convert them to a map - O(n)
var inputArr = [1,2,3,7,8,9,6];
var map = inputArr.reduce( (acc, c) => {
acc[ c ] = true; //set any truthy value
return acc;
},{});
Iterate an inputArr and see if its compliment is available in the map - O(n)
var output = [];
var mulValue = 18;
inputArr.forEach( s => {
var remainder = mulValue/s;
if ( map[s] && map[remainder] )
{
output.push( [ s, remainder ] );
map[s] = false;
map[remainder] = false;
}
});
Demo
var inputArr = [1, 2, 3, 7, 8, 9, 6];
var map = inputArr.reduce((acc, c) => {
acc[c] = true; //set any truthy value
return acc;
}, {});
var output = [];
var mulValue = 18;
inputArr.forEach(s => {
var remainder = mulValue / s;
if (map[s] && map[remainder]) {
output.push([s, remainder]);
map[s] = false;
map[remainder] = false;
}
});
console.log(output);
You can try something like this:
Idea:
Loop over the array to compute product. Use this iterator(say i) as get first operand(say op1).
Now again loop over same array but the range will start from i+1. This is to reduce number of iteration.
Now create a temp variable that will hold product and operand.
On every iteration, add value to product in hashMap.
Now loop over hashMap and remove any value that has length that is less than 2.
function sameProductValues(arr) {
var hashMap = {};
for (var i = 0; i < arr.length - 1; i++) {
for (var j = i + 1; j < arr.length; j++) {
var product = arr[i] * arr[j];
hashMap[product] = hashMap[product] || [];
hashMap[product].push([arr[i], arr[j]]);
}
}
for(var key in hashMap) {
if( hashMap[key].length < 2 ) {
delete hashMap[key];
}
}
console.log(hashMap)
}
sameProductValues([1, 2, 3, 7, 8, 9, 6])

JavaScript to split data and calculate sums

I believe what I need are two JavaScript functions. I am receiving a comma separated string that holds two types of data: 1) device name followed by 2) numeric value. These two values are separated by a comma, and each set is also separated by a comma. Example string below:
Device_A,5,Device_C,2,Device_A,10,Device_B,8,Device_B,2,Device_C,7
What I want to do is create two separate functions. The first function finds the unique device names and returns just the names in a comma separated string. The second function would calculate the sum of the numeric values for each device. The expected results from the example string above would return:
Function 1 (Device List):
Device_A, Device_B, Device_C
Function 2 (Sums per Device List):
15,10,9
The lists do not need to return in any particular order as long at they both match up. All I have successfully done at this point is return a list of unique values (including numeric values)... I'm stuck on separating the list, but still referring to device name to sum up all of the values.
Thanks in advance. Let me know if you have any questions!
Matt
You could use an object for collecting the names and count.
This edit contains a shared function and two function for the result in equal order.
function getGrouped(data) {
var array = data.split(','),
temp = Object.create(null),
i = 0;
while (i < array.length) {
temp[array[i]] = (temp[array[i]] || 0) + +array[i + 1] || 0;
i += 2;
}
return temp;
}
function getDevices(data) {
var temp = getGrouped(data);
return Object.keys(temp).sort().join();
}
function getCounts(data) {
var temp = getGrouped(data);
return Object.keys(temp).sort().map(function (k) { return temp[k]; }).join();
}
var data = "Device_A,5,Device_C,2,Device_A,10,Device_B,8,Device_B,2,Device_C,7";
console.log(getDevices(data));
console.log(getCounts(data));
When starting out on a problem like this, I think it's wise to not worry about doing it in a single loop or in a fancy one-liner at first.
A) Start out by defining what data structures you need and how to go from one format to another:
Convert my string of data to a list of keys and values
Somehow group these keys and values based on the key
Sum the values for each group
Return a list of all unique keys
Return a list of all summed values
B) Then, try to see if any of the code you've written has the potential be re-used by other parts of your application and refactor accordingly.
C) Finally, assess if there are performance bottle necks and only if there are, optimize for performance.
A. A function for each step:
// 1. From string to array of keys and values
// You already figured this one out. Split by ","!
const namesAndValuesFromString =
str => str.split(",");
// 2. Grouping by key
// Let's first make pairs:
const deviceValuePairs = devicesAndValues => {
let pair = [];
const pairs = [];
devicesAndValues.forEach(x => {
pair.push(x);
if (pair.length === 2) {
pairs.push(pair);
pair = [];
}
});
return pairs;
};
// Key value pairs are a nice starting point for constructing a grouped object:
const kvpsToDeviceValuesObj = kvps => {
const valuesByDevice = {};
kvps.forEach(([key, value]) => {
value = Number(value);
if (!valuesByDevice[key]) {
valuesByDevice[key] = [];
}
valuesByDevice[key].push(value);
});
return valuesByDevice;
};
// 3. Now, we can get to summing the values arrays
const sumValueArrays = valuesByDevice => {
const summedValuesByDevice = {};
// Loop over the objects entries
Object.entries(valuesByDevice).forEach(
([key, values]) => {
summedValuesByDevice[key] = values
.reduce((a, b) => a + b);
}
);
return summedValuesByDevice;
};
// 4. + 5. Now that we have an object with device ids as keys, and summed values inside, we can retrieve the two lists
const getDevices = Object.keys;
const getSums = Object.values;
// Running the code:
const namesAndValues =
namesAndValuesFromString("A,5,C,2,A,10,B,8,B,2,C,7");
console.log(namesAndValues);
const kvps = deviceValuePairs(namesAndValues);
console.log(kvps);
const valuesByDevice = kvpsToDeviceValuesObj(kvps);
console.log(valuesByDevice);
const sumValues = sumValueArrays(valuesByDevice);
console.log(sumValues);
const devices = getDevices(sumValues);
console.log(devices);
const sums = getSums(sumValues);
console.log(sums);
B. Refactoring!
Once you understand each of those steps, you'll start to see things that can be generalized or combined. That's where the fun starts :)
// UTILITIES
const split = del => arr => arr.split(del);
const toPairs = arr => {
let pair = [];
return arr.reduce(
(pairs, x) => {
pair.push(x);
if (pair.length === 2) {
pairs.push(pair);
pair = [];
}
return pairs;
}, []);
};
const sum = (x, y = 0) => +x + y;
const kvpsToGroups = grouper => kvps =>
kvps.reduce(
(groups, [key, value]) => Object.assign(groups, {
[key]: grouper(value, groups[key])
}), {});
// YOUR APP
const sumGrouper = kvpsToGroups(sum);
const dataSplitter = split(",");
const parseData = str => sumGrouper(toPairs(dataSplitter(str)));
// MAIN
const result = parseData("A,5,C,2,A,10,B,8,B,2,C,7");
console.log("devices:", Object.keys(result));
console.log("sums:", Object.values(result));
another way by regexs
let str = "Device_A,5,Device_C,2,Device_A,10,Device_B,8,Device_B,2,Device_C,7", obj = {}
str.match(/(\w+,[0-9]+)/g).forEach((s) => {
s = s.split(',')
obj[s[0]] = (obj[s[0]] || 0) + (Number(s[1]) || 0)
})
console.log(obj)
Something like this should do it:
var input = "Device_A,5,Device_C,2,Device_A,10,Device_B,8,Device_B,2,Device_C,7";
var output = input.split(',').reduce((accumulator, currentValue, currentIndex, array) => {
accumulator[currentValue] = (accumulator[currentValue] || 0)
+ parseInt(array[currentIndex + 1]);
array.splice(0,1);
return accumulator;
}, {});
console.log(Object.keys(output));
console.log(Object.keys(output).map(k => output[k]));

Group and aggregate an array of javascript objects

I am having some trouble rationalising and aggregating an array of objects in javascript.
Given the array
[{"description":"Bright","size":"2XL","price":10.99},{"description":"Bright","size":"XL","price":10.99},{"description":"Bright","size":"L","price":9.99},{"group":"Foos","description":"Dull","size":"XL","price":9.99},{"description":"Dull","size":"L","price":8.99},{"description":"Dull","size":"2XL","price":9.99},{"description":"Shiny","size":"XL","price":9.99},{"description":"Shiny","size":"S","price":8.99},{"description":"Shiny","size":"3XL","price":10.3},{"description":"Shiny","size":"2XL","price":9.99}]
I am trying to convert it to an array in the format (actual values may be wrong here).
[{"descriptions":"Shiny, Bright, Dull","sizeRange":"S - L","price":8.99},{"descriptions":"Shiny, Bright, Dull","sizes":"XL - 2XL","price":9.99},{"descriptions":"Dark","sizes":"S - 2XL","price":10.99}]
That is - I wish to group each set of items by price, showing the descriptions and size ranges for them.
So far this is what I have, and it seems to be working, but it just seems very cumbersome. Really I'd be more than happy to use something like lodash or underscore if it would help to rationalise the code a bit rather than using native JS.
function groupBy (array, key) {
return array.reduce(function(value, property) {
(value[property[key]] = value[property[key]] || []).push(property);
return value;
}, {});
};
function unique(array) {
return Array.from(new Set(array));
};
function getRanges(data)
{
var result = [];
// simple map of sizes from smallest to largest to use for sorting
var sizeSort = {'S':1, 'M':2, 'L':3, 'XL':4, '2XL':5, '3XL':6, '4XL':7, '5XL':8};
// group the remaining variants by price
var group = groupBy(data, 'price');
// for each variant price group
for(var price in group) {
var item = {};
item.price = price;
// get the range of sizes sorted smallest to largest
var sizes = unique(group[price].map(function(i) {
return i.size;
})).sort(function(a, b) {
return sizeSort[a] - sizeSort[b];
});
// Add single size, or first and last size.
item.sizes = (sizes.length === 1) ?
sizes.shift() :
sizes.shift() + ' - ' + sizes.pop();
// Add the descriptions as alphabetically sorted CSV
item.description = unique(group[price].map(function(i) {
return i.description;
})).sort().join(", ");
result.push(item);
}
return result;
}
Here is a version using lodash..
I think it looks more rational..
function calc(data) {
var sizeSort = {'S':1, 'M':2, 'L':3, 'XL':4, '2XL':5,
'3XL':6, '4XL':7, '5XL':8};
return _.chain(data).
groupBy('price').
map(function(f){
var sizes = _.chain(f).map('size').uniq().
sortBy(function (a) { return sizeSort[a] }).value();
return {
price: _.head(f).price,
description: _.chain(f).map('description').uniq().join(',').value(),
size: sizes.length === 1 ? _.first(sizes) : _.join([_.first(sizes),_.last(sizes)], ' - ')
}
}).
sortBy(['price']).
value();
}
//put data at end, so not having to scroll down to see code
var data = [{"description":"Bright","size":"2XL","price":10.99},{"description":"Bright","size":"XL","price":10.99},{"description":"Bright","size":"L","price":9.99},{"group":"Foos","description":"Dull","size":"XL","price":9.99},{"description":"Dull","size":"L","price":8.99},{"description":"Dull","size":"2XL","price":9.99},{"description":"Shiny","size":"XL","price":9.99},{"description":"Shiny","size":"S","price":8.99},{"description":"Shiny","size":"3XL","price":10.3},{"description":"Shiny","size":"2XL","price":9.99}];
console.log(calc(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.3/lodash.js"></script>
A vanilla JavaScript solution (with ES6 template strings)
/*
Some boilerplate functions. Listed underscore/lodash functions that
could replace them above
*/
// _.mapObject(object, reducer)
function reduceValues(object, reducer) {
let newObject = {}
for (var property in object) {
if (object.hasOwnProperty(property)) {
newObject[property] = reducer(object[property])
}
}
return newObject
}
// _.groupBy
function groupBy(arr, key) {
let reducer = (grouped, item) => {
let group_value = item[key]
if (!grouped[group_value]) {
grouped[group_value] = []
}
grouped[group_value].push(item)
return grouped
}
return arr.reduce(reducer, {})
}
// _.values
function objectValues(object) {
let values = []
for (var property in object) {
if (object.hasOwnProperty(property)) {
values.push(object[property])
}
}
return values
}
/*
Shirt specific functions and data
*/
// Mapping of shirts to their order.
let sizesToNumbers = {'S':1, 'M':2, 'L':3, 'XL':4, '2XL':5, '3XL':6, '4XL':7, '5XL':8};
// Create an intermediate summary with data instead of strings.
// This makes processing easier to write and reason about
function reduceShirtsToSummary(shirts) {
let reducer = (summary, shirt) => {
summary['descriptions'].add(shirt['description'])
let shirtSize = shirt['size']
if (!summary['smallestSize'] || sizesToNumbers[shirtSize] < sizesToNumbers[summary['smallestSize']]) {
summary['smallestSize'] = shirtSize
}
if (!summary['largestSize'] || sizesToNumbers[shirtSize] > sizesToNumbers[summary['largestSize']]) {
summary['largestSize'] = shirtSize
}
summary['prices'].push(shirt['price'])
return summary
}
return shirts.reduce(reducer, {'descriptions': new Set(), 'prices': []})
}
// Convert the shirt summary data into the "labelized" version with strings in the example
function labelizeShirtSummary(shirtSummary) {
let labelizedShirtSummary = {}
labelizedShirtSummary['descriptions'] = Array.from(shirtSummary['descriptions']).join(', ')
labelizedShirtSummary['price'] = shirtSummary['prices'][0]
labelizedShirtSummary['sizes'] = `${shirtSummary['smallestSize']} - ${shirtSummary['largestSize']}`
return labelizedShirtSummary
}
let grouped = groupBy(shirts, 'price')
let groupedAndSummarized = reduceValues(grouped, reduceShirtsToSummary)
let labelizedSummaries = objectValues(groupedAndSummarized).map(labelizeShirtSummary)
// Gives desired output
console.log(labelizedSummaries)

Categories