Which one is a better use of new Set() in JavaScript? [closed] - javascript

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
const array = [1, 2, 2, 2, 3, 4, 4, 4, 4, 5]
// first case
const filteredArray1 = new Set([...array])
// second case
const filteredArray2 = [...new Set(array)]
console.log(filteredArray1, filteredArray2)

const filteredArray2 = [...new Set(array)]
console.log(filteredArray1, filteredArray2)
// This is much suitable way of doing it since you'll be directly getting the type of the new variable as an array. In the first method you have the chance of getting the type as a set or an object.

Related

Get the splice parameter that converts array A to array B [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I want to write a method to turn array A into array B by a single splice operation, returning the format [index, deleteCount, . . items], which can be used to add or delete elements, but I need to make sure that the minimum number of elements is reached in each operation.
For example, if array A is [1, 2, 2, 3] and array B is [1, 2, 5, 3], then the return result is [2,1,5].

How to merge array of objects to objects with unique key using javaScript? [closed]

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 1 year ago.
Improve this question
How can I merge arr1 array of objects to the obj with unique key id's, My aim is to receive one array which will contain data structured like this [{ItemID: "164488786030", name:'google', promotion_id: 7, name2:'google-mt'}], I can't figure out how i can solve this using ES6 syntax, Any advice would be acceptable to me, thanks.
let arr1 = [{ItemID: "164488786031", name:'google'}, {ItemID:"164488786030", name:'facebook'}]
let obj = {164488786031: {promotion_id: 7, name2:'google-mt'}, 164488786030: {promotion_id: 9, name2:'facebook-mt'}}
This is one way of doing it:
const arr1 = [{ItemID: "164488786031", name:'google'},
{ItemID:"164488786030", name:'facebook'}] ,
obj = {164488786031: {promotion_id: 7, name2:'google-mt'},
164488786030: {promotion_id: 9, name2:'facebook-mt'}};
const res = arr1.map(e=>({ItemID:e.ItemID, name:e.name, promotion_id: obj[e.ItemID].promotion_id, name2:obj[e.ItemID].name2 }));
console.log(res)

How to return array without square brackets or commas [closed]

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 3 years ago.
Improve this question
I have an array and I want to return it with the commas and the square brackets removed from the returned value so that I can more easily analyze it and use its contents to make a sentence for a user to read better rather than a confusing script for those who don’t know JavaScript. Is there an efficient way to do this?
var a = [5, 6, 7, 3, 1];
console.log(a);
//-> [5, 6, 7, 3, 1] | I want 56731
Use .join(). This returns a string which is the array's elements joined together with nothing.
var a = [5, 6, 7, 3, 1];
console.log(a.join(""));

JavaScript, how to create an array that sequentially counts till N? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Not sure how exactly to word this, but essentially, I'm trying to write a function that if you input 5, it will return [1, 2, 3, 4, 5], if you input 7, it will return [1, 2, 3, 4, 5, 6, 7] etc etc.
It seems simple but for some reason it's just not clicking for me.
Use a for-loop that pushes to an array:
function count(n){
let arr = [];
for (let i = 1; i <= n; i++){
arr.push(i);
}
return arr;
}
console.log(count(5));
console.log(count(7));

Best way to check if an existing array is having all distinct elements or not in JavaScript? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Something like this:
let a = [2, 34, 'dafsd', null, {}];
let b = [null, null] -or- ['same','same'] -or- [100, 100]
isDistinct(a) // => true
isDistinct(b) // => false
You could take a Set of the items and check the length of the array against the size of the set. If equal, then all elements are unique.
let a = [2, 34, 'dafsd', null, {}];
console.log(a.length === new Set(a).size);

Categories

Resources