简体   繁体   English

如何使对象在数组中找到自己的索引?

[英]How to make and object find its own index in an array?

I have a simple array of objects with a id property and I need the ids to be equal to their index. 我有一个带有id属性的简单对象数组,我需要id等于它们的索引。 I tried this 我试过了

var arr = [

  {
    id: arr.indexOf(this),
    length: this.length
  },
   {
    id: arr.indexOf(this),
    length: this.length
  },
   {
    id: arr.indexOf(this),
    length: this.length
  }

]

But that doesn't work. 但这是行不通的。 Do I have to use a function outside the array to do this? 我是否必须使用数组外部的函数来执行此操作? None of the built in methods would help? 内置方法都无济于事?

 // If this.length =3 var arr = [ {length: 3}, {length: 3}, {length: 3} ] arr.forEach((item, index)=> item.id =index) console.log(arr) 

Every time you push to the array you can get the current array length and set it as id: 每次推送到数组时,您都可以获取当前数组长度并将其设置为id:

var array = []; 
array.push({id: array.length});

Also you can use a loop: 您也可以使用循环:

var array = [];
for(var i = 0; i < howeveryouwant; i++){
     array[i] = {id: i};
}

And if the initialization is static you could just set the ids: 如果初始化是静态的,则可以设置id:

 var arr = [

 {
    id:0,
    length: this.length 
 },
 {
    id: 1,
    length: this.length //this will be 0
 },
 {
    id: 2,
    length: this.length //this also will be 0
 }

]; ];

You could also use map which takes the index and array as 2nd and 3rd parameter respectively: 您也可以使用map ,它分别将index和array作为第二和第三参数:

const arr = [{title: 'A'}, {title: 'B'}, {title: 'C'}].map( (el, i, a) => ({id: i, ...el, length: a.length}));

console.log(arr);

Following answer tries to address how you can use a class to create above structure and not have to loop to mutate length. 以下答案试图解决如何使用类来创建上述结构,而不必循环更改长度。

Idea: 理念:

  • Create a function( MyList ) which has an array that holds the objects. 创建一个函数( MyList ),该函数具有一个包含对象的数组。
  • Create another function( MyTempObject ) which only holds values that are specific to object. 创建另一个函数( MyTempObject ),该函数仅保存特定于对象的值。
  • Move any common property, like length to prototype. 移动任何公共属性,例如将length更改为原型。
  • In MyList , make the array as private and create a public property that returns a copy of this array. MyList ,将数组设为私有,并创建一个公共属性,该属性返回此数组的副本。 This will ensure that value is not mutable and can only be mutated using exposed APIs. 这将确保该值是不可变的,并且只能使用公开的API进行突变。

 function MyList () { var values = []; function MyListItem(id) { this.id = id; } Object.defineProperty(MyListItem.prototype, 'length', { get: function() { return values.length; } }); Object.defineProperty(this, 'value', { get: function() { return values.slice(); } }); this.add = function(id) { values.push(new MyListItem(id)); } } var myList = new MyList(); myList.add(1); console.log(myList.value) console.log(myList.value[0].length) myList.add(2); console.log(myList.value) console.log(myList.value[0].length) myList.add(3); console.log(myList.value) console.log(myList.value[0].length) 


One reason, you might wish to use this approach is in a scenario where you add/delete entry in this array. 一个原因,您可能希望使用此方法,是在此数组中添加/删除条目的情况下。 You will have to loop again and again to update the length in every object. 您将不得不反复循环以更新每个对象的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM