This question already has answers here:
findIndex() javascript array object
(2 answers)
How do I get the index of object in array using angular?
(5 answers)
Get objects position in array
(3 answers)
Closed 1 year ago.
I have an array of object as follow :
[ {label: "<p>Opacity | %<br/></p>", customRow: false, id: 0, items: Array(13)},
{label: "Brand_hs_id", customRow: false, id: 0, items: Array(13)},
{label: "<p>PPI |</p>", customRow: false, id: 0, items: Array(13)},
{label: "Brightness | %", customRow: false, id: 0, items: Array(13)
]
I want to findIndex of object where label value matches. for example if I pass the "Brightness | %" it should return 3.
I checked
const array1 = [5, 12, 8, 130, 44];
const isLargeNumber = (element) => element > 13;
console.log(array1.findIndex(isLargeNumber));
// expected output: 3
but not sure how to do it in array of objects .
Try something like as follows:
let val = [{id:"1002", address:"usa"},
{id:"1004", address:"canada"},
{id:"1006", address:"sweden"}];
let index = val.findIndex(x => x.address === "canada");
console.log(index); //Here index will be returned
do it like this
const array1 = [{age:5}, {age:12},{age:8} , {age:130}, {age:44}];
const isLargeNumber = (element) => element.age > 13;
console.log(array1.findIndex(isLargeNumber));
Related
This question already has answers here:
Javascript: filter array of objects by array of strings
(4 answers)
Closed 1 year ago.
I have an array of objects named profiles:
[{id: 0, subcategories: ['A', 'B', 'C']}, {id: 1, subcategories: ['A']}]
I have an array named tags: ['B']
I want to filter profiles, only keeping profiles that have a value in tags. In this case, profile with id = 0 would be saved.
This is what I have tried so far and it doesn't seem to be working:
let filtered_profiles = profiles.filter((profile) =>
tags.includes(profile.subcategories)
);
This is my approach. If profiles subcategories includes at least one tag, push it into the result object.
let profiles = [{id: 0, subcategories: ['A', 'B', 'C']},
{id: 1, subcategories: ['A']}];
let tags = ['B'];
let result = [];
profiles.forEach(function (p){
let copied = false;
tags.forEach(function (t) {
if (p.subcategories.includes(t) && !copied) {
result.push(p);
copied = true;
}
})
});
console.log(result);
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 2 years ago.
I have an array of object and want to reorder objects inside array Is there any way to do that?
var obj = [ {start: 12, end: 14}, {start: 2, end:8}]
I should check if start date of first object is greater than second object's start date and change objects order, The result in this case should be =>
var obj = [ {start: 2, end:8}, {start: 12, end: 14}]
I know about sorting inside object but cant find way to reorder the whole object.
Use your expeceted property to conduct compared function
var obj = [
{ start: 12, end: 14 },
{ start: 2, end: 8 },
]
const res = obj.sort((a, b) => a.start - b.start)
console.log(res)
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 years ago.
I am trying to convert the "count" key from each object within the test array into a new array so I end up with something like
newCount =[0,0,0,0]
const test = [
{
id: 0,
count: 0,
image: "",
text: 'Some text about finn'
},
{
id: 1,
count: 0,
image: "",
text: 'Some text about daphne'
},
{
id: 2,
count: 0,
image: "",
text: 'Some text finn'
},
{
id: 3,
count: 0,
image: "",
text: 'Some text daphne'
}
]
You can use Array.prototype.map() on your test array to extract the count property value of each object:
const newCount = test.map(t => t.count);
Hopefully that helps!
You can use a map on the array:
const newarray = test.map(item => item.count);
Array map documentation
This question already has answers here:
Sorted a javascript array of objects by an object property
(6 answers)
Closed 5 years ago.
I currently have an array object (not sure if this is the accurate name) that is comprised of nested key value pairs. I want to be able to sort this by the values within the nested objects.
For example:
var ObjArray = [
{ id = 1,
info = {
number = 4,
name = "foo"
}
},
{ id = 4,
info = {
number = 12,
name = "bar"
}
},
{ id = 9,
info = {
number = 2,
name = "fizz"
}
}
];
So ideally I could sort this object based on the 'number' property, and the resulting array object would have sub objects ordered by the number value within the info.
I've found a similar question (Sorting an object of nested objects in javascript (maybe using lodash?)) but doesn't account for another level of nested objects.
The sorting function needed is
ObjArray.sort((a,b) => a.info.number - b.info.number);
This will sort them ascending
For descending :
ObjArray.sort((a,b) => b.info.number - a.info.number);
var ObjArray = [{
id: 1,
info: {
number: 4,
name: "foo"
}
},
{
id: 4,
info: {
number: 12,
name: "bar"
}
},
{
id: 9,
info: {
number: 2,
name: "fizz"
}
}
];
ObjArray.sort((a,b) => a.info.number - b.info.number);
console.log(ObjArray);
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 6 years ago.
I want to order a javascript list containing object according to a key,
var a = [{order: 1, item: 'shoes'}, {order: 3, item: 'jeans'}, {order: 2, item: 'shirt'}, {order: 1, item: 'coat'}]
Here I want to order according to order from lower to upper like:
var b = a.filter(data => data.order) // have some check here
How can I do that ?
you need to sort by the property so you can use a functional style to curry the key, and return a function that Array.prototype.sort can use.
var clothes = [
{ order: 1, item: 'shoes' },
{ order: 3, item: 'jeans' },
{ order: 2, item: 'shirt' },
{ order: 1, item: 'coat' }
]
const sortBy = key => (a, b) => a[key] - b[key]
console.log(
clothes.sort(sortBy('order'))
)
<script src="http://codepen.io/synthet1c/pen/WrQapG.js"></script>
If you are using lodash, you can do (docs)
_.sortBy(list, el => el.order);
If you are not (and I believe you should), you can take a look at their source code for hints ;)