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
Related
This question already has answers here:
Javascript sort array of objects using array of priority
(2 answers)
Closed 1 year ago.
var array = [{value:"13",type:"Fruit"},{value:"61",type:"Animal"},
{value:"19",type:"Fruit"},{value:"71",type:"Animal"},
{value:"12",type:"Fruit"},{value:"15",type:"Fruit"},
{value:"11",type:"Plant"},{value:"10",type:"Fruit"},
{value:"16",type:"Plant"}]
What is the best/optimized way to sort this array such that I get all elements of type Fruit first and then the elements of type Animal (by picking elements from end of array).
expectedOutput = [{value:"10",type:"Fruit"},
{value:"15",type:"Fruit"},{value:"12",type:"Fruits"},
{value:"19",type:"Fruit"},{value:"13",type:"Fruit"},
{value:"71",type:"Animal"},{value:"61",type:"Animal"},
{value:"16",type:"Plant"},{value:"11",type:"Plant"}]
Note:- I don't need to sort it Alphabetically. It must be sort depending upon the specific type.
You can store the order of the type properties in an array, then subtract the index of the type property when sorting to determine precedence:
const order = ["Fruits", "Animal"]
var array = [{value:"13",type:"Fruit"},{value:"61",type:"Animal"},
{value:"19",type:"Fruit"},{value:"71",type:"Animal"},
{value:"12",type:"Fruit"},{value:"15",type:"Fruit"}]
const sorted = array.sort((a,b) => order.indexOf(a.type) - order.indexOf(b.type))
console.log(sorted)
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%;}
This question already has answers here:
remove common elements of two arrays in jquery
(4 answers)
Closed 3 years ago.
Lets say i had two arrays
a.["mark#example.com", "bob#example.com"]
b.["carl#example.com", "mark#example.com", "bob#example.com", "josh#example.com"]
How can i make a third array, using the variables that are present in the second one, but not on the first one?
the desired output should be this:
c.["carl#example.com", "josh#example.com"]
const arrayA = ["mark#example.com", "bob#example.com"]
const arrayB = ["carl#example.com", "mark#example.com", "bob#example.com", "josh#example.com"]
const arrayC = arrayB.filter(x => !arrayA.includes(x))
console.log(arrayC)
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.
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));