This question already has answers here:
Check if every element in one array is in a second array
(10 answers)
Closed 2 years ago.
I am trying to filter my entites by tags array, that can look like this:
const tags = ["tag1", "tag2"];
Every entity has property tags, that can have existing tags, for example:
["tag1", "tag2", "tag3"];
Or:
["tag1", "tag2"];
I need to compare if the tags array and the tags array of entity has the same values.
So far I have tried this code, but it returns an entity even if the two arrays dont have the same values (I'm guessing the includes() function is to blame).
tags.every((tag: any) => doc.tags.includes(tag));
Any ideas how can I check if the arrays have the same values?
Thank you in advance.
You can also compare the length as well
tags.every((tag: any) => doc.tags.includes(tag)) && tags.length === doc.tags.length;
Related
This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 6 years ago.
Assuming I've a JSON object like this:
var myObj = {
'question1': {
'option1': 'foo',
'option2': 'bar',
'option3': 'baz'
},
'question2': {
...
},
'question3': {
...
}
};
And since its children always has a number in its keys, I want to do a loop and concatenate the loop's index to the object keys, and get the values in the dot notation method...
So, I guess to get the values, I need to do some thing like this:
myObj.'question'+i
How can I do the concatenation right?
Simply do
myObj['question'+i]
This is because the dot operator would not accept string with it as per javascript. So you will have to use square brackets instead which is often used to access properties of an object dynamically.
This question already has answers here:
How to create unique keys for React elements?
(16 answers)
Closed 3 years ago.
How can I generate custom IDs for input without using any packages/moduls outside of React?
I found this example for unique keys using index, but as one user pointed out, while React documentation says you can do it that way:
const todoItems = todos.map((todo, index) =>
// Only do this if items have no stable IDs
<li key={index}>
{todo.text}
</li>
);
It's not wise to use index for this. Is there any other way I could generate unique IDs with React? Thanks!
In the react docs you link it's says: "We don’t recommend using indexes for keys if the order of items may change."
So if you change the orders in your array (like sorting them) and you haven't a unique propertie just give them one initial (when you create the array or directly after getting the array)
todos.forEach((todo, index) => todo.index = index);
This question already has answers here:
Nodejs: url.parse issue with arrays inside query
(2 answers)
Closed 6 years ago.
I'm working in a MEAN app, in which I want to pass an array value to url and fetch values from mongodb. Actually it's a lat and lng. My url should look like:
http://localhost:8080/search?type=latlng&value=[0.123, 0.456]
And have to fetch data related to that lat lng. How to do that?
You cannot pass literal array objects in the URL like you propose. There are workarounds however. You must either separate the data into {key}={value} pairs within the URL, or use a POST request.
GET: http://localhost:8080/search?type=latlng&lat=0.123&lng=0.456
http://localhost:8080/search?type=latlng&coords[]=0.123&coords[]=0.456
POST: http://localhost:8080/search
data: { type: 'latlng', value: [0.123, 0.456] }
This question already has answers here:
Determine whether an array contains a value [duplicate]
(18 answers)
Closed 7 years ago.
I have array of name:
$scope.myArray with some values.
Now I want to check an Item is present in array or not,
If it is not present then I want to push that item into array and If that item is already in array then I don't want to push it.
Please tell me how to do this ?
if($scope.myArray.indexOf(item) === -1) {
$scope.myArray.push(item)
}
You can read about indexOf for more details. Also, checkout underscore js for some readily available functions.
This question already has answers here:
Javascript multiple email regexp validation
(7 answers)
Closed 7 years ago.
I need to split a value from a cell into separate array values. I am taking a list of emails from my spreadsheet and need to use the addCommenters() method.
Spreadsheets cell value: email1#email.com, email2#email.com, email3#email.com ... etc.
I need to split around the ", " properly to pass the array into addCommenters().
What is the programmatic way to do this?
If you get the value of that cell (using .getValue())
var emails = sheet.getRange(range_here).getValue()
and then apply the split() method
var commentersArray = emails.split(",")
you will have an array you can pass to the .addCommenters() method.
file.addCommenters(commentersArray);