JavaScript - Compare two array and insert if not exists [closed] - javascript

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 2 years ago.
Improve this question
I have two arrays. For example:
var array1 = ["Q3","Q4","Q5","Q6","Q7","Q8"];
var array2 = ["Q5","Q6","Q7"];
The output of second array should be,
[0,0,"Q5","Q6","Q7",0];
I want to compare the first array with the second and fill the missing values with Zero 0.
array1 is the primary array and, for now, array2 will have this 3 values initially.

Given the example arrays a simple map -> includes would work:
var array1 = ["Q3","Q4","Q5","Q6","Q7","Q8"];
var array2 = ["Q5","Q6","Q7"];
console.log(array1.map(i => array2.includes(i) ? i : 0))

Related

Filtering an array with an empty array [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 months ago.
Improve this question
const arrFilter = [{id:1},[],[]];
how can I filter the empty array in the above example, so that the result would be the object with an id only
You can use the length of the array returned from Object.keys() to determine if objects, arrays or strings are empty however it won't work on numbers.
With that in mind, try this assuming that
all empty objects, arrays and strings should be omitted
everything else stays
const arrFilter = [{id:1},[],[], "a string", "", 1, 0];
const nonEmpties = arrFilter.filter(
(item) => typeof item === "number" || Object.keys(item).length > 0
);
console.log(nonEmpties);

Ho can i make an array of only those values, that are non-unique in initial array in js? [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 12 months ago.
Improve this question
i have this array:
var arr = ["apple","potato","carrot","tomato","grape","banana","potato","carrot","carrot"];
As you can see, we have "potato" 2 times in this array, and also "carrot" 3 times in this array. How can i create a new array, that will content only "potato" and "carrot" values, so only values that are not single/unique in initial array?
Like this:
["potato","carrot"];
How it can be done in js?
Check this one liner:
[...new Set(arr.filter(e => arr.filter(a => a === e).length > 1))];
arr.filter(e ...) - filter array for duplicates
[...new Set(...))] - create array with unique values using set and spread syntax
Working snippet:
var arr = ["apple","potato","carrot","tomato","grape","banana","potato","carrot","carrot"];
console.log([...new Set(arr.filter(e => arr.filter(a => a === e).length > 1))]);

JavaScript : How create proportionality rule function with array parameter [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 2 years ago.
Improve this question
I want to create an Array from another by applying the proportionality rule.
For example:
Array1 = [15,550,76,3,400,230]
I take the maximum value from Array1 which is equal in my array 550
Now, with a function, I want to create a second array Array2 with a value equal to 10 which corresponds to 550 in the first array, like Array2 = [?,10,?,?,?,?]
I want to apply the proportionality rule function to determine the other values of Array2 to get Array2=[0.27,10,1.38,0.05,7.27,4.6]
Any help please how can I create the function with array param?
Thank you.
const array1 = [15,550,76,3,400,230]
function proportionalityRule(array){
const maxValue = Math.max(...array)
return array.map(element => (element/maxValue) * 10)
}
proportionalityRule(array1)
// (6) [0.2727272727272727, 10, 1.3818181818181818, 0.05454545454545455, 7.272727272727273, 4.181818181818182]

Remove non-numeric items in a listu using regex [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 3 years ago.
Improve this question
I am working with an array in Javascript that contains several IDs in them but I would like to filter out all non-numeric entries using regex and return that array of just numbers. For example, I have myArray = ['131125150138677','CI%20UW%20SYSTEMS%20S','040964100010832'] where I want to get rid of the second item in the list since it's non-numeric.
So use Filter and test to see if they are numbers
var myArray = ['131125150138677', 'CI%20UW%20SYSTEMS%20S', '040964100010832']
var filtered = myArray.filter(Number)
console.log(filtered)
var filtered2 = myArray.filter(s => s.match(/^\d+$/))
console.log(filtered2)

Horizontal concatenation of two array [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 3 years ago.
Improve this question
how to do horizontal concatenation of two arrays in javascript?
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
I want output like :
c = [[1,2,7], [3,4,8], [5,6,9]]
You can use map and use index to access corresponding value from second array
var a = [[1,2],[3,4], [5,6]]
var b = [7,8,9]
let final = a.map((v,i)=> v.concat(b[i]))
console.log(final)

Categories

Resources