简体   繁体   English

使用Javascript中的值从多维数组中删除元素

[英]Removing elements from multi dimensional array using value in Javascript

I have a multidimensional array is :- 我有一个多维数组是:-

serialNos = [{id:'12',serial:'DAS245'},{id:'13',serial:'DAS246'},{id:'15',serial:'DAS247'}]

the ids and serials are random and i use push function to push them inside the array as i get those. id和序列是随机的,当我得到它们时,我使用push函数将它们推入数组。

serialNos.push({id:'16',serial:'DAS248'});

Now, the thing is at times I have to remove elements from this array but all i get is serial not the id ie, DAS248. 现在,事情有时是我必须从此数组中删除元素,但我得到的只是序列而不是ID,即DAS248。 How do I delete it in easy steps? 如何通过简单的步骤将其删除? I dont want to go thru iteration method as it takes too much memory and is not efficient. 我不想使用迭代方法,因为它占用太多内存并且效率不高。 Also I want to restore order i mean indexes of serialNos in ascending order without any gaps. 我也想恢复顺序,我的意思是serialNos的索引按升序排列,没有任何间隙。

NOTE : ids and serials both are unique 注意:id和序列号都是唯一的

Platform : Javascript 平台:JavaScript

Assuming you don't have any repeated serial numbers, you could make serialNos an object instead of an array: 假设您没有任何重复的序列号,则可以将serialNos对象而不是数组:

serialNos = {
    'DAS245': {
        id: 12,
        serial: 'DAS245'
    }
    // etc
};

This way, you can just delete the property using delete serialNos['DAS245']; 这样,您可以使用delete serialNos['DAS245'];删除属性delete serialNos['DAS245']; .

With this method, there's no "sorting" this object, but when you iterate through it, you can use this solution . 使用此方法,没有“排序”该对象,但是当您遍历它时,可以使用此解决方案

If you can use an object from the get go, that would be best (see @Travesty3 's answer). 如果可以一开始就使用对象,那将是最好的选择(请参阅@ Travesty3的答案)。 If you are stuck with your array, do something like this. 如果您卡在阵列中,请执行以下操作。 You'll incur cost of indexing the array's objects only once rather than each time you want to find something. 您将只为数组对象建立一次索引,而不是每次要查找某项时都会产生索引费用。

var serialNos = [
  { id: 12, serial: 'DAS245' },
  { id: 13, serial: 'DAS246' },
  { id: 13, cereal: 'DAS249' },
  { id: 15, serial: 'DAS247' }
];

var moBetter = indexByKey(serialNos, 'serial');
console.log(moBetter.DAS246);

/**
* Given an array of objects, each with a unique attribute,
* this will return an object, indexed by those keys.
* @param {Object[]} arrOfObjects
* @param {String} key The unique identifier inside each object.
* @return {Object}
*/
function indexByKey (arrOfObjects, key) {
  var index = {},
    i = 0,
    max = arrOfObjects.length;

  // iterate through the array
  for (; i < max; i++) {

    // check to see if the object has the key
    if (arrOfObjects[i].hasOwnProperty(key)) {

      // make sure we haven't already indexed this key
      if (index.hasOwnProperty(arrOfObjects[i][key])) {
        console.error('Hey, wait.  These aren\'t unique!');
      } else {

        // If all is well, index this object by the key.
        index[arrOfObjects[i][key]] = arrOfObjects[i];
      }

    } else {
      console.error('This does not have the key, so it cannot be indexed.', arrOfObjects[i]);
    }
  }

  return index;
}

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

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