Eliminating smaller values in an array [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an array like so:
const dynamicNumber = 4
const array = [10, 15, 20, 25, dynamicNumber]
So what i would like to do, is eliminate all the terms that are smaller than that dynamic number.
So in this case, I would like to create a copy of the array and remove the values which are greater:
const newArray = [4]

Check out array.filter:
const dynamicNumber = 4
const array = [10, 15, 1, 2, 20, 25, dynamicNumber]
const filteredArray = array.filter(number => number < dynamicNumber)
console.log(filteredArray)

Related

JavaScript Filter And IndexOf Remove Duplicate [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 days ago.
Improve this question
I am looking for question "Remove a duplicate element from given array and return unique element".
I've tried this code:
var arr = [4, 3, 6, 7, 10, 4, 5, 2];
function removeDuplicates(arr) {
return arr.filter((item, index) => arr.indexOf(item) === index);
}
console.log(removeDuplicates(arr));
There is a way with Set
const removeDuplicates = (arr) => [...new Set(arr)]
console.log(removeDuplicates([1,1,1,2,2,3,4,5,5, 4, 2 , 3,6]))
Your code is OK, but you can simply use Set, a data structure that allows you to create a collection of unique values.
const uniqueArr = [...new Set(arr)];

Compare 2 variables with comma separated values in JavaScript [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have 2 variables say a=1,2,3,4 and b=1,2 , I want to compare these 2 variables and form a new variable with common values, please help me
Assuming you meant:
a="1,2,3,4"
b="1,2"
You could break those comma delimited values into arrays and then get the intersections of the arrays. Here's an example:
const ArrayA = a.split(",");
const ArrayB = b.split(",");
const intersection = ArrayA.filter(value => ArrayB.includes(value));
const commonCSV = intersection.join(",");
var a = [1, 2, 3, 4];
var b = [1, 2];
// Take unqiue values from a and concat with unique values from b
const occurrenceMap = [...new Set(a)].concat([...new Set(b)]).reduce((map, el) => {
map.set(el, map.get(el) ? map.get(el) + 1 : 1);
return map;
}, new Map());
const common = [];
// Iterate over the map entries and keep only the ones that occur more than once - these would be the intersection of the 2 arrays.
for (entry of occurrenceMap) {
const [key, val] = entry;
if (val > 1) {
common.push(key);
}
}
console.log(common);

Convert object to an array and assign its position accordingly [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I need help to solve the problem below. I have this object that has 4 quarters from Q1 to Q4; however, there is one missing which is Q3. The goal is to convert the object to an array and assign the values to where its index should be. In this case, Q1 should be in 0 index, Q2 is in 1 index, and so on. Q3 is missing so it should be assigned to 0. Could someone help me with this? Thanks.
let obj = {Q1: 13, Q2: 5, Q4: 3};
// here is the output
[13, 5, 0, 3]
You could use a destructuring for the properties with default values and get an array.
const
convert = ({ Q1 = 0, Q2 = 0, Q3 = 0, Q4 = 0 }) => [Q1, Q2, Q3, Q4];
let obj = { Q1: 13, Q2: 5, Q4: 3 },
result = convert(obj);
console.log(result);
I do this:
let obj = {Q1: 13, Q2: 5, Q4: 3};
console.log (Object.values(Object.assign({Q1:0,Q2:0,Q3:0,Q4:0},obj)))

How do I create a function that can generically calculate product in any object's set of values? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm tasked with trying to create a function that will return the product of key value pairs of any given array.
function productOfValues(someObject) {
//write your code here!
**return [testObject.a * testObject.b * testObject.c];***
}
let testObject = {
'a': 5,
'b': 12,
'c': 3
}
productOfValues(testObject)
The asterisked line above indicates the code I've tried to implement, but it does not work. Additionally, to complete this question, I'm supposed to write the function more generically so that the function will calculate the product of any object created. Kind of lost here.
You first need to get all the values from your object using Object.values() and than just calculate the product using Array.reduce() of the values that you just got.
const testObject = {
'a': 5,
'b': 12,
'c': 3
};
const calculateProductOfObjectValues = (obj) => {
const values = Object.values(obj);
return values.reduce((a,b) => a * b);
};
console.log(calculateProductOfObjectValues(testObject))

Given a list of positive and negative numbers. What is an elegant solution to tell which is closest to zero? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Say you have a list of numbers:
var list = [4, -12, 18, 1, -3];
What is a more elegant solution to finding the value closest to zero without nesting a whole lot of if/else statements?
use reduce:
list.reduce((pre,cur) => Math.abs(pre) > Math.abs(cur) ? cur : pre)
This is what you could do with a single traversal of the array.
function findClosestToZero(arr) {
if (arr.length === 0) {
return;
}
var closeNumber = Math.abs(arr[0]);
arr.forEach(function(number) {
if (Math.abs(number) < closeNumber) {
closeNumber = Math.abs(number)
}
});
return closeNumber;
}
var arr = [-5, 98, -4, 7, 9, 213, 4, -2, 1];
console.log(findClosestToZero(arr));
use sort :
list.sort((a,b) => Math.abs(a)-Math.abs(b))[0];
get the absolute value of all numbers
Math.abs()
sort the number in ascending order
Array.sort()
The first number is the required answer.

Categories