Get the last two positions of an array [duplicate] - javascript

This question already has answers here:
How do I get the last 5 elements, excluding the first element from an array?
(9 answers)
Closed 6 years ago.
I would like to know how can I obtain the last two elements of this array like this:
array = [1,5,7,8,10,12,23,24];
I am trying to obtain with slice() function but it doesn´t work for me, beacuse I always I want to obtain the last two positions.
array = [23,24];
I hope that anyone can help me in this question.
Regards!

Use Array#slice with negative index.
var array = [1,5,7,8,10,12,23,24];
console.log(array.slice(-2));
You can learn more about this method also here.

array = [1,5,7,8,10,12,23,24];
console.log(array.slice(-2))
Use slice -2

aray.prototype.splice() also
Note: Splice it will update the original array also
var array = [1,5,7,8,10,12,23,24];
console.log(array.splice(-2));

Related

How to make all objects into one array and remove duplicates? [duplicate]

This question already has answers here:
Remove duplicate values from JS array [duplicate]
(54 answers)
Closed 1 year ago.
I have this object, and I want Remove duplicates and make it into one array
var sports = [
['basketball','fotball','racing'],
['fotball','basketball','swimming'],
];
What is the best way to get it like this:
['basketball','fotball','racing','swimming'],
The flat() will make the sports into one array, and is probably the best option here?
Just have to remove the duplicates any tips?
use below:
[...new Set(sports.flat())]
Here sports.flat() flatten out the array. See the doc here
and new Set() will make them unique. See the doc here

Is there a way to add each value to the first one in the for loop in JS? [duplicate]

This question already has answers here:
Easy way to turn JavaScript array into comma-separated list?
(22 answers)
Closed 2 years ago.
I have 4 different values, when looping through an array. Is it possible to add the values together like you would in Java's StringBuilder append?
I want something like this when doing a console.log():
28.0334307,-25.872523799999996, 28.031527552564445,-25.87632233243363
Now I am just getting it one for one like this when doing a console.log():
28.0334307
-25.872523799999996
28.031527552564445
-25.87632233243363
Here is my code:
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
for(var item in coordinates)
{
console.log(item);
}
you can get a string in-line separated by a comma with join() method, try this:
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(', '));
Try this:
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(' '));
var coordinates = [28.0334307, -25.872523799999996, 28.031527552564445, -25.87632233243363]
console.log(coordinates.join(' '));
The browser consoles displays an array like that in to be more readable. It's not an actual structural representation of how an array is. What you want is basically a string created by joining the elements of the array.
Array.join method can be used for this:
coordinates.join("'")
Use JavaScript array join() method to display values separated by comma.
The join() method returns the array as a string.
The elements will be separated by a specified separator. The default separator is a comma (,).

Remove duplicates in an array of objects in javascript [duplicate]

This question already has answers here:
How to remove all duplicates from an array of objects?
(77 answers)
Closed 2 years ago.
I have an array of objects which is similar to:
Questions-answers [{"questions":"Q_18002_Error_message","answers":"Yes"},{"questions":"Q_18002_Error_message","answers":"No"},{"questions":"Q_18001","answers":"No"}]
I want to delete {"questions":"Q_18002_Error_message","answers":"Yes"} because I have a new updated answer to the question Q_18002,
I am a newbie in Javascript and I am stuck on how to delete the duplicate elements but based on the questions only and delete the old ones and leave the new objects. I hope that I made it clear.
You may build up the Map (with Array.prototype.reduce()) using questions as a key, overwriting previously seen values, then extract array of unique records with Map.prototype.values():
const src = [{"questions":"Q_18002_Error_message","answers":"Yes"},{"questions":"Q_18002_Error_message","answers":"No"},{"questions":"Q_18001","answers":"No"}],
result = [...src
.reduce((r,o) => (r.set(o.questions,o), r), new Map)
.values()
]
console.log(result)
.as-console-wrapper{min-height:100%;}

How to change value in array with index in react native [duplicate]

This question already has answers here:
How can I update state.item[1] in state using setState?
(25 answers)
Closed 4 years ago.
I have an array in my state:
state = {
some_array: [1,1,1,3,6,3,6,23], // sorry for the syntax it's changed
}
Now I want to change value in that array that has index let's say 4, and in this case that would be number 6, or if I want to change index 1, that would be second number or array.
I know this is probabbly very simple but I'm just very confused.
If you need more info please comment.
Thanks!
I think that you can to use next code:
const some_array = [...this.state.some_array]
some_array[indexHere] = yourValue
this.setState({some_array:some_array})
This example --- true way for FP in react.

Get data from JS array of objects [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
Probably very easy question, but I couldn't find solution.
How to get data from this object?
That how it looks in consolo.log()
UPDATE:
Thank you for you answers.
That what I used before and it worked, but when I try on this array it returns error.
console.log(array[1].data);
Output picture
UPDATE2:
So I tried to make it a text, but I couldn't.
console.log(tempArray);
console.log("String: " + tempArray.toString());
console.log("Stringify: " + JSON.stringify(tempArray));
Here is output:
Stringify attempt result
Maybe there is something wrong with how I create this array.
let tempArray = [];
And in the loop
tempArray.push({"id": id, "data": data.routes[0].geometry});
Thank you,
Dmitry
That's an array of objects, so you would get elements from it like so:
console.log(obj[i].data)
Where i is the element (numbered 0 through 2) that you want to access.

Categories