This question already has an answer here:
Javascript Array.prototype.filter() not working
(1 answer)
Closed 3 years ago.
Why doesn't array.map work?
My code is:
let myArray = [000,111,222,333,444,555,666,777,888,999];
myArray.map((value) => {
return = 1000 - value;
});
console.log(myArray);
The result is:
[0, 111, 222, 333, 444, 555, 666, 777, 888, 999]
Calling map returns a new array. It doesn't modify the old one in place. Do this instead:
let myArray = [000,111,222,333,444,555,666,777,888,999];
let myNewArray = myArray.map((value) => {
return 1000 - value;
});
console.log(myNewArray);
Related
This question already has answers here:
How can I sort a javascript array of objects numerically and then alphabetically? [duplicate]
(5 answers)
Closed last month.
If I have the object,
const obj = { Peter: 3, Jeremy: 2, Chris: 1, Adam: 2 };
I want to compare object values, and sort them in a numerical order.
Therefore, I tried
let answer = Object.keys(obj);
answer.sort((a,b) => {
return obj[b] - obj[a];
})
The output is ['Peter', 'Jeremy', 'Adam', 'Chris'].
I want to sort Jeremy and Adam in Alphabetical order as they have same values.
The output I want is ['Peter', 'Adam', 'Jeremy', 'Chris']
How do I approach to this answer?
you can use a condition for case where values are same to sort based on alphabetical order of keys
const obj = { Peter: 3, Jeremy: 2, Chris: 1, Adam: 2 };
let answer = Object.keys(obj);
answer.sort((a,b) => {
if (obj[b] == obj[a]) return a.localeCompare(b)
return obj[b] - obj[a];
})
console.log(answer)
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
const obj = { Peter: 3, Jeremy: 2, Chris: 1, Adam: 2 };
const result = Object.entries(obj) // get array of [key, value]
// sort by value descending, then alphabetically by key
.sort(([a,b],[c,d])=>d-b || a.localeCompare(c))
.map(([i])=>i) // extract just the key
console.log(result)
This question already has answers here:
How to convert key-value pair object into an array of values in ES6?
(5 answers)
Closed 1 year ago.
I need to convert a object
{score: 77, id: 166}
to an array,
[77,166]
I tried,
Object.keys(obj).map((key) => [obj[key]]);
but seems like, it returns as 2 arrays.
[[77][166]]
You just had an extra pair of square brackets in your code
const obj = {score: 77, id: 166};
const result = Object.keys(obj).map((key) => obj[key]);
console.log(result)
You can use also use Object.values(obj) to achieve this result
const obj = {
score: 77,
id: 166
}
const result = Object.values(obj)
console.log(result);
this will return you an array of values
This question already has answers here:
Array map function doesn't change elements
(4 answers)
Why does this map function not mutate the values in the original array?
(3 answers)
Closed 2 years ago.
The problem is that I cannot subtract each index of an array from its element(number), using the array.prototype.map() method. I expected the subtraction to be valid, but it is not.
Here is the code:
const whiteSp = [ 5, 11 ];
whiteSp.map(function (ele, i) {
console.log(ele, i) // 5 0, 11 1
console.log(ele - i) // 5, 10
ele = ele - i;
return ele;
});
console.log(whiteSp) // expected [ 5, 10 ], but got [ 5, 11 ]
The second console.log indicates the computation has been made as seen by the value 10, but returns 11 for some reason.
I have also tried 'return ele - i' without its above line, but still does not work.
Ciao, you could try something like this:
let whiteSp = [ 5, 11 ];
whiteSp = whiteSp.map((ele, i) => { return ele - i; });
console.log(whiteSp)
and remember that map function returns a new array so you have to do whiteSp = whiteSp.map....
You need an assignment of the mapped values.
const
whiteSp = [5, 11],
result = whiteSp.map((ele, i) => ele - i);
console.log(result);
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.
Improve this question
Here is the array of objects that is to be push to an array
[{"a":11,"b":21},{"a":31,"b":41},{"a":10,"b":20}]
How to achieve this array below from the above
"array": [
[
[
11,
21
],
[
31,
41
],
[
10,
20
]
]
]
Use Array.prototype.map()
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
var array1 = [{"a":11,"b":21},{"a":31,"b":41},{"a":10,"b":20}];
var array2 = [array1.map(item=>[item.a, item.b])];
console.log(array2);
Map it
let arr = [{"a":11,"b":21},{"a":31,"b":41},{"a":10,"b":20}]
let result = [arr.map(({a,b}) => [a,b])];
console.log(result);
Use reduce to form an array and then Object.values to extract the object prop values for each sub array.
const arr = [{
a: 11,
"b": 21
}, {
"a": 31,
"b": 41
}, {
"a": 10,
"b": 20
}];
const result = arr.reduce((acc, x) => {
const values = Object.values(x);
acc.push(values);
return acc;
}, [])
console.log({
array: result
});
[].concat(array.map((val)=>Object.values(val)))
If all you want to do is push each element of B into A you can do A.concat(B).
If you want to make a new array with all the values your can
c = ([]).concat(A,B)
For your array of objects with values, you could
c = []; for ( vals in b ) c.concat(Object.values(vals);
This question already has answers here:
How to find the array index with a value?
(12 answers)
Get the index of the object inside an array, matching a condition
(16 answers)
Closed 3 years ago.
Im trying to determine the location of a in a array. Im not really sure how to handle this case
const expect = require('chai').expect;
const answers = require('../src/arrays');
describe('arrays', function() {
let a;
beforeEach(function() {
a = [ 1, 2, 3, 4 ];
});
it('expect determine location of a in array', function(expect) {
expect(answers.indexOf(a, 3)).to.eql(2);
expect(answers.indexOf(a, 5)).to.eql(-1);
});
Try this!
var array = [ 1, 2, 3, 4 ];
function finding_index(element) {
return element === 2;
}
console.log(array.findIndex(finding_index));