find first index of array in string javascript [duplicate] - javascript

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
There is a string variable that has a path like /app/something/xx/4/profile
besides there is an array of string like **
const arr=[
{name:'xx',etc...},
{name:'yy',etc...},
{name:'zz',etc...}
]
I want to find the first index of array that the string variable has the name in simplest way.

Use Array.findIndex which :
returns the index of the first element in the array that satisfies the provided testing function. Otherwise, it returns -1, indicating that no element passed the test.
const str = "/app/something/xx/4/profile";
const arr = [{ name: "xx" }, { name: "yy" }, { name: "zz" }];
const index = arr.findIndex(e => str.includes(e.name));
console.log({ index });

Related

Remove duplicate content in array from another array [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 4 months ago.
In Javascript, I have this
var a = ["1","2","3"]
var b = ["3","4","5"]
assuming "b" reads "a" and removes the 3 because it is repeated, how can I get this?
var c = ["4","5"]
Thank You!
You can use set and spread operators.
set has property to have all unique value object.
Below we are using Set constructor passing values using spread operator and it return an object so to get an array we are again passing that object with spread operator.
let a = ["1", "2", "3"]
let b = ["3", "4", "5"]
let c = [...new Set([...a, ...b])];
console.log(c);
Check if value exists on another array using includes()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
You can remove values by using filter()
Docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
To do what you want:
const a = ["1","2","3"]
const b = ["3","4","5"]
const c = b.filter((value) => !a.includes(value))
This has been answered before and is in detail: How to get the difference between two arrays in JavaScript?

I need to reference the value of the same object on another value [duplicate]

This question already has answers here:
How can a JavaScript object refer to values in itself? [duplicate]
(8 answers)
Closed 1 year ago.
Hello i have an array of objects. Like this:
const obj = [{
count: 10,
value: count*2 // the previous count
}]
How can i reference 'count' on 'value' without having to find the index, or is a way to find the index of 'obj'?
You could take a getter with a reference to the same object.
const
objects = [{
count: 10,
get value () { return this.count * 2; }
}];
console.log(objects[0].value);

Javascript includes() in list of list not working [duplicate]

This question already has answers here:
Array.includes() to find object in array [duplicate]
(8 answers)
Check if an array contains any element of another array in JavaScript
(32 answers)
Closed 2 years ago.
var coords = [
[0,0],[5,0],[0,5],[5,5],[8,5],[8,0],[40,5],[5,54],[6,7],[40,0],[8,54]
]
console.log(coords.includes([0,0]))
The result here is false, and I am unsure why. Please help
Array.prototype.includes compares the values using the strictly equal operator. Arrays are stored by reference and not by value. [] === [] will never be true, unless you are comparing the same array stored in your array.
A way to solve this issue is using Array.prototype.some
var coords = [
[0,0],[5,0],[0,5],[5,5],[8,5],[8,0],[40,5],[5,54],[6,7],[40,0],[8,54]
]
console.log(coords.some(coordinate => {
const [x, y] = coordinate
// This is the value you are comparing
const myCoords = [0, 0]
return x === myCoords[0] && y === myCoords[1]
}))

How can I check if a value exists inside an array of objects in javaSript? [duplicate]

This question already has answers here:
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 3 years ago.
For eg.
[
{
x:1,
y:2
},
{
x:10,
y:20
},
]
How can I check if x exists in both of the objects inside the array?
DESIRED OUTPUT:
if x doesn't exist in even one object inside the array ---> false
else ---->true
I have tried using the array.prototype.find() method but not able to find the right logic to get the desired output.
You could check the objects with Object.hasOwnProperty and the wanted property and take Array#every for checking all elements of the array.
var array = [{ x: 1, y: 2 }, { x: 10, y: 20 }],
result = array.every(o => o.hasOwnProperty('x'));
console.log(result);
You can use the operator in for checking if a property exists in the object, this along with the function every.
let arr = [{x:1,y:2},{x:10,y:20}];
console.log(arr.every((o) => "x" in o))
console.log(arr.every((o) => "z" in o))

How to convert an Array to Array of Object in Javascript [duplicate]

This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Convert array of strings into an array of objects
(6 answers)
Closed 3 years ago.
I want to convert an Array like:
[ 'John', 'Jane' ]
into an array of object pairs like this:
[{'name': 'John'}, {'name':'Jane'}]
Please help me to do so..
Try the "map" function from the array:
const output = [ 'John', 'Jane' ].map(name => ({name}));
console.log(output);
You can use the instance method .map() on a JS list Object as follows :
let list = ['John', 'Jane']
list = list.map(x => {
return({name: x});
});
console.log(list);

Categories

Resources