I'm having trouble with the following code (will explain after) within a Vue SPA component:
const accountArray = Array.from(Object.values(this.accounts)[0]) // [0 is the sub-array I need, and won't change]
const urlId = this.$route.params.id // 3
const i = accountArray.findIndex(account => account.id === urlId) // -1
console.log(i) // -1, as it didnt found any matches
console.log(urlId) // 3
The this.accounts is a computed property that returns an Observable object, and it works as expected (the 0 index is the sub-array of interest), hence my looping on it. The accountArray variable also returns the array that I want to iterate over (it has an account with id = 3), but when it comes to the iteration predicate, it ALWAYS returns -1 when it should be returning 2. Also, if I hard-code 3 instead of urlId, it returns the 2 that I'm looking for. Why?
When I print the accountArray, the output is the following:
I've made sure that this is not a Vuex reactivity problem, because I can refresh the page and it still returns me the arrays I need. What I'm missing here?
Thanks in advance!
You're using === comparison which expects the same value and same data type which is not true for your case. Your account.id is number and your urlId coming from route params is a string (3 !== "3").
Try converting route params id to a number before comparing it to account.id.
const urlId = +this.$route.params.id // 3
Notice + sign in front of this.$route.params.id used to convert a string to a number.
As an alternative, you could compare it using == which would return true for your 3 == "3" case, but I suggest you to stick to the first advice.
Related
I have this snippet of code below and don't quite understand the output
function repeatStringNumTimes(str, num) {
if (num <0) {
return ""
} else {
return Array(num+1).join(str)
}
}
console.log(repeatStringNumTimes("abc", 3));
I would have expected the output to be "abcabcabc" though if I console.log(repeatStringNumTimes("abc", 3)) in JS Bin it produces "abcabc"?
If I specify Array(3) - Would it not concatenate the string 3 times? Why only 2 in this instance?
If I specify Array(3) - Would it not concatenate the string 3 times?
Why only 2 in this instance?
console.log([1,2,3].join('abc'))
// outputs 1abc2abc3
Note that 'abc' is the separator for the join between the 3 elements, and so it appears twice, not 3 times.
Therefore, if you create an empty array, it shows 'abc' twice, separating 3 empty strings:
console.log(Array(3).join('abc'))
// outputs abcabc
Also note that there is String.repeat()
console.log('abc'.repeat(3))
// outputs abcabcabc
look when i run your code the right output appears so your code has no bugs, and when you specify Array(3) the output will be "abcabc" your code is working well
If you will have a look into the Array#join documentation, It will join the array items based on the passed separator in the join method. Ideally, It will not consider the join before the first and after the last element in an array.
console.log(Array(3)) // [undefined, undefined, undefined]
console.log([undefined, undefined, undefined].join('abc')); // 'abcabc'
As you want to fill the array with the string, Correct method is Array#fill which changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length) and then join the elements from an array with the help of Array.join() method.
console.log(Array(3).fill('abc').join(''));
I have two arrays one of it is having user id and another one is having user ids. Those arrays are as follows.
1)The array which is having user id.
data[key].effective_employees Which is eaqual to [2].
Now I have another array which is having numbers of employee ids which is as follows.
data2[0].id Which is eaqual to [2,22,21].
And now I am trying to see whether the array two has number in array 1 I am using the following logic to see whether it is working or not.
if ((/^\d+$/.test(_.intersection([data2[0].id.toString()], data[key].effective_employees)))) {
let isElem = _.contains(returnStackFilterd, value);
if (isElem == false) {
returnStackFilterd.push(value);
}
} else {
returnStackFilterd = _.without(returnStackFilterd, value);
}
But this is showing true for the number 2 if the array two is having 22. Psudo code of what is happening with it is as follows.
if([2]is in[22,21]){ it is printing true} I want false here as the number two is not in the second array. The second array contains 22 and 21 which is not eaqual to 2
How do i solve this problem? The above psudo code should print false.
Let's break down your test expression and see why it doesn't work.
First off, we know that data[key].effective_employees is [2]. data2[0].id might be [2, 22, 21] or [22, 21]. If I'm understanding your question correctly, you want the whole test expression to return true in the first case and false in the second case.
Rebuilding your test expression from the bottom up, the innermost expression we find is this:
data2[0].id.toString()
This is a string with the value '2,22,21' or '22,21', depending on which case we are talking about. Next, you wrap this string in an array:
[data2[0].id.toString()]
So now we have ['2,22,21'] or ['22,21']. Note the quotes; in either case, it is an array with a single element that is a string.
Next, you take the intersection of this array with data[key].effective_employees, which we know is [2]:
_.intersection([data2[0].id.toString()], data[key].effective_employees)
So this expression is effectively
_.intersection(['2,22,21'], [2])
or
_.intersection(['22,21'], [2])
You are always taking the intersection of two arrays, where the first contains a single string and the second contains a number. They can't have any elements in common, so that's always going to produce an empty array ([]).
Finally, you test whether that empty array matches a regular expression:
/^\d+$/.test(_.intersection([data2[0].id.toString()], data[key].effective_employees))
// is effectively the same as
/^\d+$/.test([])
Regular expressions are supposed to be matched against a string, not an array. JavaScript is very lenient in situations like these and will coerce the value you're passing to a string. That means that the value [] is first converted to the empty string '' before being matched to the regular expression /^\d+$/. The empty string does not contain any digits, so this test always returns false.
This is why your test doesn't work as intended. However, let's take a few steps back, because you seem to be doing many things you don't need to do. Why convert arrays to strings (and then back to array)? Why match against a regular expression, if you just want to know whether two arrays have elements in common?
The following, simpler expression will give you the elements that data[key].effective_employees and data2[0].id have in common:
_.intersection(data[key].effective_employees, data2[0].id)
This will evaluate to either [2] or [], depending on whether data[key].effective_employees contains the number 2 or not.
I suggest saving the result of this expression to a variable, because it makes your code easier to read. Let's call it commonIds:
const commonIds = _.intersection(data[key].effective_employees, data2[0].id)
Now you can formulate different conditions, based on what exactly you want this intersection to be like. My impression is that you just want it to be nonempty (i.e., at least one element in common). In that case, you can compare its length to zero:
if (commonIds.length > 0) {
// code for when there is an overlap
} else {
// code for when there is no overlap
}
As a final note, I recommend assigning your base expressions data[key].effective_employees and data2[0].id to variables as well. Again, this makes your code more readable, and it also ensures that you need to change only one line of code if those base expressions change. Putting it all together:
const key = 'x';
const data = { [key]: {
effective_employees: [2],
}};
const data2 = [{
id: [2, 22, 21],
}];
const userId = data[key].effective_employees;
const employeeIds = data2[0].id;
const commonIds = _.intersection(userId, employeeIds);
if (commonIds.length > 0) {
console.log('userId appears in employeeIds');
} else {
console.log('userId does not appear in employeeIds');
}
<script src="https://underscorejs.org/underscore-umd-min.js"></script>
If data[key].effective_employees is the number 2, and data2[0].id is the array [2, 22, 21], the expression to test whether data2[0].id contains data[key].effective_employees is:
data2[0].id.includes(data[key].effective_employees)
From your original question, data2[0].id.toString() coerces the array to a string 2,22,21, which is no use to you. You also do not need to use Underscore for this.
I found solution on this link.
Get array's depth in JavaScript
Due to inadequate reputation, I could not comment to ask for explanation through comments.
function getArrayDepth(value) {
return Array.isArray(value) ?
1 + Math.max(...value.map(getArrayDepth)) :
0;
}
let testRy = [1,2,[3,4,[5,6],7,[8,[9,91]],10],11,12]
console.log(getArrayDepth(testRy))
Could you please explain why you are adding 1 and why you are using the Math.max function and the spread operator in the function above? How does this function work?
First, a primer on recursion:
Recursion ultimately tries to solve the most basic form of the problem you can have and then gradually narrow down any complex problem to the most basic form. So you need the following (probably doesn't make complete sense until you read all of it):
You need to solve the base case.
the base case also serves as a terminal condition. You want to stop calling the function recursively at some point. Once you reach the base case you don't need to recurse any more.
You need a reduction step. You start with a big problem and you aim to go to the base form of it and solve it (the base case from 1.). If the current form is not the base, then it's not solvable - you need to reduce the problem a bit and call the function recursively.
So, in this case, the base case is that you get a value that is not an array. Since it's not an array, it doesn't have depth, hence you return zero. SOLVED! That is it.
However, what happens if you do get an array? Well, any array will have some depth. So, you can count one then get the contents of the array. That's the reduction step - you've reduced it from "I have an array of unknown depth" to "I have the contents of my array". When you recursively call getArrayDepth with the contents, you will re-evaluate whether you have an array or not and count appropriately. If you sum all the times you've had an array with depth of at least 1, you get the depth.
So far, we can solve the following things
Input: 42
Output (depth): 0
Why?: It's the base case - it's not an array, so we return 0 and don't recursively call the function.
Input: [42]
Output (depth): 1
Why?: We have an array ->
count depth of 1 add the result of the recursive call with the contents 42 -->
it's the base case - it's not an array, so we return 0 and don't recursively call the function.
Going back, we've had 0 and 1 which totals 1.
Input: [[42]]
Output (depth): 2
Why?: We have an array ->
count depth of 1 and recursively call with the contents [42] -->
we have an array -->
count depth of 1 and recursively call with the contents 42 --->
it's the base case - it's not an array, so we return 0 and don't recursively call the function.
Going back, we've had 0, 1 and 1 which totals 2.
And so on.
Now, as for why you use Math.max and Array#map. Since arrays can have many elements, you use .map to call getArrayDepth on each of them. This will cause further recursion to work it out but at the end you will get an array of all the depths of the elements, so ["a", ["b"], [["c"]]] will be transformed into [1, 2, 3]. Since you need to get the total depth, you need the highest number - this is achieved by using Math.max with spread syntax to return the maximum value.
The individual values in testRy are:
1
2
[3,4,[5,6],7,[8,[9,91]],10]
11
12
On first execution of getArrayDepth with testRy, Array.isArray(value) will return true. So because this is true, we know we have at least depth 1 so that is why the 1+ is there. It then adds that to the the maximum value of calling getArrayDepth on each element in testRy. So that would look like:
getArrayDepth(1) => Array.isArray() false => 0
getArrayDepth(2) => Array.isArray() false => 0
getArrayDepth([3,4,[5,6],7,[8,[9,91]],10]) => Array.isArray() true => 1 + next max
so we hit another array, now we have at least another depth but we need to check if there are arrays in this array, so the cycle continues with this new array (it still has to finish testRy, but with the way it executes, it will go through this one first):
getArrayDepth(3) => Array.isArray() false => 0
getArrayDepth(4) => Array.isArray() false => 0
getArrayDepth([5,6]) => Array.isArray() true => 1 + next max
and this repeats until the end of testRy.
Translating the code to English would sound something like this:
Is the input an array?
yes - get the largest (Math.max) depth of each of the elements (value.map(getArrayDepth)) and increment it by 1 (1 +) to account for the current input array.
no - there is no array, thus no depth, return 0.
The spread operator is used to supply the array return value of value.map(getArrayDepth) as separate arguments. Since that is what Math.max expects.
I am completing a fairly complicated process in node and for it to work properly I need to compare the value of a key in an object to an array. I have checked to make sure I have no async issues and am simply using a indexOf to get the index of the object's key in the array. I have checked the typeof each item and both return "object" "object". Here is an example check that I am making.
var cID = [55cebe83d0b3d];
var item = { _id: 55cebe83d0b377d,
_client: 55cebe83d0b3d,
institution_type: 'test' }
var cIndex = cID.indexOf(item._client)
and then cIndex always equals -1 regardless of it really exists within cID. If I console log item._client it is 55cebe83d0b3d which is exactly what is stored within cID but still returns -1. Any ideas? I also have the data coming from MongoDB and the data is going through body-parser before hitting this function.
I suppose that hex numbers are not js-native. Turn them into hex strings. E.g. '5cebe83d0b377d' instead of 5cebe83d0b377d.
Update
Mea culpa, see Hiyper's comment.
I have this code that fetches data and puts it into an array:
this.$httpGetTest(this.test.testId)
.success(function (data: ITestQuestion[]) {
self.test.qs = data;
});
It works and populates the array starting with self.test.qs[0].
However many times my code references this array (which contains a list of questions 1...x)
I must always remember to subract 1 from the question number and so my code does not look clear. Is there a way that I could place an entry ahead of all the others in the array so that:
self.test.qs[0] is null
self.test.qs[1] references the first real data for question number 1.
Ideally I would like to do this by putting something after the self.test.qs = and before data.
Push values at start of array via unshift
self.test.qs.unshift(null);
You need to use Splice(), It works like:
The splice() method changes the content of an array, adding new elements while removing old elements.
so try:
self.test.qs.splice(0, 0, null);
Here mid argument 0 is to set no elements to remove from array so it will insert null at zero and move all other elements.
Here is demo:
var arr = [];
arr[0] = "Hello";
arr[1] = "Friend";
alert(arr.join());
arr.splice(1,0,"my");
alert(arr.join());
You can start off with an array with a null value in it, then concat the questions array to it.
var arr = [null];
arr = arr.concat(data);
You could do something like:
x = [null].concat([1, 2, 3]);
Though there isn't anything wrong with doing something like:
x[index-1]
I'd prefer it to be honest, otherwise someone might assume that the index value returned is 0 based.