Excuse the beginners question, I am still very new to javascript.
Background:
I am building a chart that needs to have 2 different arrays but their length must match. In order to get these arrays to match in length, I need to determine values that match and values that don't using dates (this is already accomplished - just giving insight!)
Problem:
I need to create a new array that has the same length as array 2 (example below), while keeping the existing data from array 1.
For example: The array that I want to append to looks something like this:
[1, 5, 10, 35, ...etc]
And has a length of 20.
The other array (the one where I've mapped values that match/do-not) looks like this:
[{date, matched: false}, {date, matched: true}, {date, matched: true}, {date, matched: false}, ...etc]
And has a length of 30.
Basically, what I am trying to do is for every value where matched = false, I wish to insert a 0 in the first array (or create a new array where the value = 0). However, where matched is true, I wish to keep the existing value in the first array.. I am struggling with using loops as the lengths and indexes are different amongst the two.
Can anyone please provide some insight on how to achieve this?
Thanks! :D
CodePudding user response:
I'm not sure if this is what you had in mind, but you can use Array#map (grabbing the index) to find whether or not to return the actual value, or zero.
array1.map((a,i) => array2[i].matched ? a : 0)
let array1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
let array2 = [{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false},{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false},{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false},{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false},{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false},{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false},{date:'2022/04/25', matched: false}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: true}, {date:'2022/04/25', matched: false}]
let matched = array1.map((a,i) => array2[i].matched ? a : 0);
console.log(matched)
CodePudding user response:
What have you tried?
What happens when array2, an array of objects, finds false values of matched for which there is no index in array 1? What value is stored in array1 when array2 is greater and matched true
arr1 [1, 5, 10, 35]
// 0 1 2 3
arr2 [
{date, matched: false},
{date, matched: true},
....,
{date, matched: false} // indice 30
];
arr1 [0,5,10,35,undefined?,null?,another_data?,0]
You can use a for loop but forEach() is simpler, let's see!
array2.forEach((element,index)=>{
// Body of the loop and this code is executed for each element
if(!element.matched){
// at each iteration index represents the position of the element en array2
array1[index] = 0;
}
})
The code walks the array of objects through each element and its index, then at each iteration we check the matched value of each element ({matched:true,..}).
If matched is false (!true) then in the index position that has element in array2 we will assign 0 in array1.
The downside of this is that only false values are set, which is going to leave an array with undefined values.
Another option if you only want to keep the length of array1 could be to iterate over this array and not array2
array1.forEach((element,index)=>{
if(!array2[index].matched) array1[index] = 0;
});
This way array1 keeps its length but we discard the rest of the elements of array2.
Note that I only use index and not element, I could have set the value of element to 0 instead of array1[index] which is the same in the end, but I wanted to be more pedagogical. Since we are not interested in element, let's use a common for.
for(let index = 0; index < array1.length; index ){
if(!array2[index].matched) array1[index] = 0;
}
But it all depends on what you are going to do with the leftover values of array2.
Now if you want to store a value in the undefined positions of array1 for which matched==true, then use an else in the condition.