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)
Related
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
This question already has answers here:
remove all elements that occur more than once from array [duplicate]
(5 answers)
How to remove all duplicates from an array of objects?
(77 answers)
Completely removing duplicate items from an array
(11 answers)
Closed 1 year ago.
I have an array of objects with input like below
var jsonArray1 = [{id:'1',name:'John'},{id:'2',name:'Smith'},{id:'3',name:'Adam'},{id:'1',name:'John'}]
The id 1 appears twice and I would like to drop all duplicate records .i.e. my output should look like
[{id:'2',name:'Smith'},{id:'3',name:'Adam'}]
Can you please guide/direct me in how I can drop all duplicate records
Create Map from an array where key of the Map is your desired id, duplicated won't be preserved (only last occurrence will). After just take the values from Map.
Removing all of occurrences of duplicates (this is what OP wanted):
const input = [{id:'1',name:'John'},
{id:'2',name:'Smith'},
{id:'3',name:'Adam'},
{id:'1',name:'John'}]
const res = input.filter((x, i, arr) =>
arr.filter(e => e.id === x.id).length === 1)
console.log(res)
Preserving 1 occurrence of duplicates:
const input = [{id:'1',name:'John'},
{id:'2',name:'Smith'},
{id:'3',name:'Adam'},
{id:'1',name:'John'}]
const unique = [...new Map(input.map(item => [item.id, item])).values()]
console.log(unique)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I am an array of hashes and I want to convert it to array of values something like this
array of hashes [{text: "James"},{text: "developer"}]
convert this to
array of values ["James", "Developer"]
Please help me achieve this in javascript.
Using Object.values, you can get the values of each object.
const input = [{text: "James"},{text: "developer"}];
const output = input.flatMap((item) => Object.values(item));
console.log(output);
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:
Sum similar keys in an array of objects
(15 answers)
How to merge duplicates in an array of objects and sum a specific property? [duplicate]
(2 answers)
Closed 3 years ago.
I have an object array of the form
var arrayA = [
{value:3000, date:"01/01/2020"},
{value:4000, date:"02/13/2020"},
{value:5500, date:"01/01/2020"},
{value:2300, date:"03/03/2020"},
{value:1200, date:"02/13/2020"},
{value:4000, date:"03/03/2020"}
];
I want to add values that have the same date and come up with a new array as below.
var _new_arrayA = [
{value:8500, date:"01/01/2020"},
{value:5200, date:"02/13/2020"},
{value:6300, date:"03/03/2020"}
];
I do not know where to start, therefore i don't have any implementation code