How you do search all object values inside an array? [duplicate] - javascript

This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
Closed 4 years ago.
How do I search all values inside all objects in an array object in javascript?
const array = [
{
id: 145,
name: "Test"
},
{
id: 241,
name: "Array"
}
]
if I run a function it returns find('a') it returns the second object or if i run find(1) it returns both of the object

something like this
var matches = [];
for(i=0;i<array.length;i++)
{
if(array[i].name.indexOf('a') > -1)
{
matches.Push(array[i]);
}
}
return matches;
This isn't valid javascript as such, but that is how you would write something like this in javascript.

Related

How do i access an element of an object that is inside an array and that array is inside an object [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed last year.
I need a small Help in JS? i want to access mail_address of this nested object, how will I do it
const data =
{ members:
[ { id : '7d8c03e88a8386f6453340c1db56'
, mail_address : 'trial.om'
, uniqsl : 'c6cce01'
} ] }
const data = {
members: [
{
id: '7d8c03e88a8386f6453340c1db56',
mail_address: 'trial.om',
uniqsl: 'c6cce01',
}
]
}
console.log(data.members[0].mail_address) // Prints "trail.om"
members is a array of objects, but it has one single element. You access this element at the index 0.

I am getting this type of array when i try to push object into an array in jquery [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 3 years ago.
while inspecting in browser, I am getting this type of array and I am unable to access 0th index of array.
function xyz()
{
var obj={
name: "abc",
age: "20"
var VArr=[];
VArr.push(obj);
}
[]
0:Array(1)
0:{name:"abc",age:"20"}
First acess to the array by VArr[0] inside this u have your objecet to access your object property use dot notation like this
console.log(VArr[0].name)
Full code
function xyz(){
var obj={
name: "abc",
age: "20"
}
var VArr=[];
VArr.push(obj);
console.log(VArr[0].name)
}
xyz()

In javascript, how to convert string array into array of object? [duplicate]

This question already has answers here:
Convert an array to an array of objects
(4 answers)
Closed 4 years ago.
I have the following array of string that needs to be turn into array of object with the keys value and label
languages: ["Arabic", "Irish"]
0:"Arabic"
1:"Irish"
length: 2
If I wanted to make this array into an array of object like the following:
languages = [
{ value: 'Arabic', label: 'Arabic' }
{ value: 'Irish', label: 'Irish' }
]
How will I do that? Can someone please help?
You can use Array.map():
var languages = ["Arabic", "Irish"];
var res = languages.map(item => ({'value':item, 'label':item}));
console.log(res);
You can also use Array.forEach() as:
var languages = ["Arabic", "Irish"];
var res = [];
languages.forEach(item => res.push({'value':item, 'label':item}));
console.log(res);

Angular2 Typescript how to retrieve an array from inside an object [duplicate]

This question already has answers here:
Find a value in an array of objects in Javascript [duplicate]
(20 answers)
Closed 5 years ago.
When I do console.log of the following:
console.log(this.categories);
I get this structure:
which was created from this json from the api:
[
{
"id":"dff0bb3e-889f-43e6-b955-52cc7203952f",
"name":"Wood",
"category_types":[
{
"id":"49b31f43-d98d-43c8-9798-088ec6914fe5",
"name":"Solid"
},
{
"id":"8592716a-ffd5-4e97-ba9e-6a64ba6e48f1",
"name":"Engineered"
}
]
},
{
"id":"6b2b6914-6c64-4389-a327-be3f08fd066d",
"name":"Carpet",
"category_types":[
{
"id":"0e2c8473-90fb-4806-a6e7-2389eeb0f9e4",
"name":"Loop pile"
},
{
"id":"3c762653-4f0d-42d2-a84e-133c7051c95b",
"name":"Cut pile"
},
{
"id":"0997d4dc-e886-46ef-83b4-d90c4fb72553",
"name":"Cut \u0026 loop pile"
}
]
}
]
Given that I have the value 'Wood' in a string variable called value, how do I get hold of the category_types array for the Wood object?
console.log(this.categories[value]);
returns undefined.
You want to find object that has name of 'Wood', then get category_types. Array.prototype.find returns first value that meets the condition in the provided test function. In your case, it could look like this:
this.categories.find(value => value.name === 'Wood').category_types
You can use Array.filter:
let wood = this.categories.filter(category => {
return category.name === "Wood";
})[0];
This will filter your categories to retrieve the ones with name wood, then you take the first one. If you didn't find anything with name === "Wood" in your array, then it will be undefined.

Find object by condition [duplicate]

This question already has answers here:
How to search JSON tree with jQuery
(11 answers)
Closed 5 years ago.
I have the following object "list":
{
...
2: {id: 35, name: 'dog Sharik'},
3: {id: 36, name: 'cat Murzik'}
...
}
Need to find object which consist 'cat' word in name.
What is the best way to do it (does Jquery can help with it?)
Iterate the loop and find it.Use a for loop to iterate the obj and return when the name property of the object contains the word cat.Here obj is your original object
CODE
function findmatching(obj,word){
for(var key in obj){
if(obj[key]['name'].indexOf(word)!=-1){
return obj[key]
}
}
}
var mymatchingvalue=findmatching(userinput,word)
You can use lodash/underscore.js
_.filter(list, function(d) { return _.includes(d.name, 'cat') })

Categories