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

4
  • If u have an iterative way which you think is effecient please let me know that too, am interested. I just want the most efficient way possible. Commented Feb 14, 2014 at 21:09
  • 2
    This array has one dimension. :) Commented Feb 14, 2014 at 21:17
  • serial:DAS248 is invalid unless DAS248 references something.... I'd imagine it's supposed to be a string literal, i.e.: serial:"DAS248". Commented Feb 14, 2014 at 21:22
  • yea those are strings i evaluate while pushing, let me edit those, thanks Commented Feb 14, 2014 at 21:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
@JinendraKhobare you don't use push with objects. Start by learning about the basics of the language: developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
Okie, if not push then how do i add objects dynamically in the array?
@JinendraKhobare: serialNos['DAS245'] = {id: 12, serial: 'DAS245'};
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;
}

4 Comments

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, 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.
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 I hear you. I upvoted your answer before I wrote mine. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.