0

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.

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 i.e., 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.

NOTE : ids and serials both are unique

Platform : Javascript

scrappedcola
  • 10,021
  • 1
  • 28
  • 41

2 Answers2

5

Assuming you don't have any repeated serial numbers, you could make serialNos an object instead of an array:

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

This way, you can just delete the property using delete serialNos['DAS245'];.

With this method, there's no "sorting" this object, but when you iterate through it, you can use this solution.

Community
  • 1
  • 1
Travesty3
  • 14,336
  • 6
  • 52
  • 95
  • the ids are unique. but while pushing am facing difficulties syntax errors. Can u tell me how to push them? if i have ids and serials in variables say serialID and serialVal? – Jinendra Khobare Feb 14 '14 at 21:13
  • @JinendraKhobare you don't use `push` with objects. Start by learning about the basics of the language: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects – scrappedcola Feb 14 '14 at 21:23
  • Okie, if not push then how do i add objects dynamically in the array? – Jinendra Khobare Feb 14 '14 at 21:25
  • @JinendraKhobare: `serialNos['DAS245'] = {id: 12, serial: 'DAS245'};` – Travesty3 Feb 16 '14 at 01:51
1

If you can use an object from the get go, that would be best (see @Travesty3 's answer). 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;
}
reergymerej
  • 2,070
  • 2
  • 23
  • 28
  • My answer isn't suggesting to turn the array into an object *every time you want to find something*. I was suggesting to use an object instead of an array from the beginning. So you would never create an array at all. – Travesty3 Feb 16 '14 at 16:54
  • @Travesty3, sorry for the implication that you suggested to convert to an array repeatedly. That's not what I meant. I agree that using an object is ideal and this shows how he can convert it. – reergymerej Feb 18 '14 at 13:10
  • That's just the thing. This is suggesting to take the collection as an array, and *convert* it to an object. To clarify, I would suggest to never create an array. Build it as an object from the start, when you initially populate the collection. Side note, please don't take this in an attacking tone. You've got the checkmark, so I guess your answer is more of what the OP was looking for. – Travesty3 Feb 18 '14 at 16:37
  • @Travesty3 I hear you. I upvoted your answer before I wrote mine. :) – reergymerej Feb 18 '14 at 16:41