Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have an array like this:
var words = { 'love': 4; 'peace': 10; 'war':3; 'family':13; 'dog':19, 'life':7 };
What is the fastest way to get the top 2 keywords (family and dog in this case) ?
Take the keys and sort them with their values descending and take the first 2 elements.
var words = { 'love': 4, 'peace': 10, 'war': 3, 'family': 13, 'dog': 19, 'life': 7 },
top2 = Object.keys(words).sort(function (a, b) {
return words[b] - words[a];
}).slice(0, 2);
document.write('<pre>' + JSON.stringify(top2, 0, 4) + '</pre>');
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to get this code to return a new array using .map(). For some reason I cannot figure out how to make the anonymous function work with it. Here is my code. It just returns undefined, smh.
const numbers = [2, 7, 9, 171, 52, 33, 14]
const toSquare = num => num * num
// Write your code here:
function squareNums(numArr) {
numArr.map(num => {
return (num ** 2);
})
};
const numberArray = squareNums(numbers);
console.log(numberArray);
Your code is correct, it's just your squareNums function doesn't return anything. In fact, that means it returns undefined, which is what you see printed in the console.
Return the result of numArr.map instead:
function squareNums(numArr) {
- numArr.map(num => {
+ return numArr.map(num => {
return (num ** 2);
})
};
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I am currently trying to simplify the following code written in Javascript
let x = 5;
let y = 10;
let z = 19;
let answer;
arrays = ["x", "y", "z"];
if("x" in arrays){
answer = x;
}
if("x" in arrays && "y" in arrays){
answer = x + y
}
The code will continue on and on for all the possible combinations of x, y and z. What I want to know is how I can simplify the above block of code and achieve the same result.
Thank you.
Instead of individual variables, create an object with your name/value pairs. Then it's trivial to iterate over the names in the array, use them to access the values in the object, and add them together. In fact, this is most naturally expressed as a reduce operation:
let vals = {
x: 5,
y: 10,
z: 19
};
let answer = ["x", "y", "z"].reduce((a, k) => a + vals[k], 0);
console.log(answer);
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 2 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
How can we split or divide an array into two new arrays?
SingleARR = [7,5,6,4,3,2,4,5,4,2,8,8];
one array should have values that don't repeat
and the other has values that repeat. Moreover, both new arrays should have different elements from each other.
First, count the frequencies. Then filter it by the frequency if it is one then that does not repeat and push it into one array. Then again filter it by the frequency, if it is greater than 1 then it repeats and pushes
let a = [7, 5, 6, 4, 3, 2, 4, 5, 4, 2, 8, 8];
let ret = a.reduce((p, c) => {
if (!p[c]) p[c] = 1;
else p[c] += 1;
return p;
}, {});
let x = [];
let y = [];
console.log(ret);
for (prop in ret) if (ret[prop] === 1) x.push(+prop);
for (prop in ret) if (ret[prop] > 1) y.push(+prop);
console.log(x);
console.log(y);
it into another array.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Here are two arrays:
const a = [1, null, 2, null]
const b = [null, null, 3, 4, null]
Note: The length of a and b is not fixed.
I want elements of b replace elements of a by index if the element is not null.
expect value:
[1, null, 3, 4]
const result = b.map((el, i) => el === null ? a[i] : el);
result.push(...a.slice(b.length));
Just map to a new array.
You can use map() function
var x=b.length+1;
b.concat(a);
var c=b.map(function(el, index) {
if(a[index]!=null && b[index]==null){
return b[index]=a[index];
else{
return b[index]
}
console.log(c.slice(0,x));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have this array
test = [3, 2, 2, 1];
I want to be able to get this array
result = [3, 1, 2, 0]
The idea is the same as: Javascript: Sort array and return an array of indicies that indicates the position of the sorted elements with respect to the original elements
but incrementing the position value every time there is two elements on "test" with the same value.
You can pass a comparison function to sort:
var test = [3, 2, 2, 1];
var result = [];
for(var i = 0; i != test.length; ++i) result[i] = i;
result = result.sort(function(u,v) { return test[u] - test[v]; })
console.log(result) // [ 3, 1, 2, 0 ]