Why does an indexer property in a class need a private array field and not just an integer telling the object its index?
Date : March 29 2020, 07:55 AM
I hope this helps you . The class needs to know exactly what the index is referencing. An integer containing just the index doesn't tell the class what object it's supposed to retrieve with that index, especially if that class isn't itself a collection (but this can be the case even with a collection type depending on its implementation). For example, if myObject is an instance of MyClass and MyClass isn't a collection, what exactly would myObject[index] refer to? The calling code needs to know this (since it expects a return type that contains a SomeMethod() method), but only MyClass can tell the calling code exactly how its indexer works since the indexer is part of its class definition. public interface IFooList
{
Foo this[int i] { get; set; }
}
public IList<Foo> FooList { get; } = new List<Foo>();
public Foo this[int i]
{
get { return FooList[i]; }
set { FooList[i] = value; }
}
|
Javascript: Finding an object's index in an array, not knowing the index, only the object
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You could iterate over the data and then check the length of the keys and every key, if it has the same content. var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }],
search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
keys = Object.keys(search),
index = -1;
data.some(function (a, i) {
if (Object.keys(a).length === keys.length && keys.every(function (k) { return a[k] === search[k]; })) {
index = i;
return true;
}
});
console.log(index);
var data = [{ "field": "ingredients", "gte": "egg", "lte": "egg" }, { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" }, { "field": "ingredients", "gte": "hvetemel", "lte": "hvetemel" }],
search = { "field": "ingredients", "gte": "bakepulver", "lte": "bakepulver" },
keys = Object.keys(search),
index = data.findIndex(a =>
Object.keys(a).length === keys.length && keys.every(k => a[k] === search[k]));
console.log(index);
|
How to update Array object index when i delete a array object item from array in angualrjs?
Date : March 29 2020, 07:55 AM
Hope that helps After you remove element you can use forEach() loop to change indexes. var item = [
{index:1, name: 'miraje'},
{index:2, name: 'alamin'},
{index:3, name: 'behestee'},
{index:4, name: 'arif'},
{index:5, name: 'riad'}
];
item.splice(1, 1)
item.forEach((e, i) => e.index = i + 1)
console.log(item)
|
How to bind one array of class on the same page but give each set of controls for one object same index ID
Date : March 29 2020, 07:55 AM
I wish did fix the issue. The answer was on the Comment by Stephen Muecke So much You make your partial an EditorTemplate (by naming it NotificationTemplate.cshtml and placing it in the /Views/Shared/EditorTemplates/ folder) and then use @Html.EditorFor(m => m.yourCollectionProperty) to generate the correct html for each item in the yourCollectionProperty
|
Array > Object: how to index an array of objects with reduce, omitting the index from nested element in result (es201
Date : March 29 2020, 07:55 AM
hop of those help? If you want to make and object with id as key and remaining object as value. You can try following using Rest ParametersES2017 const peopleArray = [{"id": "Antoine","country": "France"},{"id": "Alejandro","country": "Spain"}];
const result = peopleArray.reduce( (ac, o) => {
ac[o.id] = Object.assign({}, o);
delete ac[o.id].id;
return ac;
}, {});
console.log(result);
const peopleArray = [{"id": "Antoine","country": "France"},{"id": "Alejandro","country": "Spain"}];
const result = peopleArray.reduce( (ac, {id, ...rest}) => Object.assign(ac, {[id]: rest}), {} );
console.log(result);
|