0

I have an object of array's like so:

{
    "numbers": [
        {"id":"11111"},
        {"id":"22222"}
    ],
    "letters": [
        {"id":"aaaaa"},
        {"id":"bbbbb"},
        {"id":"33333"}
    ]
}

I'd like to figure out how to move id: 33333 from the letters object to the numbers object.

The only want I know how to move things is within it's own "dimension" using the .move(old_index, new_index)

Could anyone help me out to move from a different dimension?

I know a possible way would be to cycle through each item in the array, look for the specific id, and then push it to the other array and delete it from the previous one but I am trying to see if there is a better way.

bryan
  • 8,879
  • 18
  • 83
  • 166
  • possible duplicate of [Find object by id in array of javascript objects](http://stackoverflow.com/questions/7364150/find-object-by-id-in-array-of-javascript-objects) from there simply remove from array and push into the other – abc123 May 26 '15 at 16:36

2 Answers2

1

First, your numbers and letters properties are actually arrays, not objects.

Since they are arrays, and id:33333 is the last element in the letters array, you can use .pop() to remove it.

I don't know what the parent object is named, but you can do something like:

parentObject.numbers.push(parentObject.letters.pop())

Are you looking for a more programmatic way to do this? What if the elements you want to move are not the last element in the array? Let me know and I'll keep going.

EDIT:

So if you're trying to remove values from an array that aren't at the beginning or end (ie: .shift() or .pop()), then you can use .splice() to remove the elements from any point in the array.

For instance: [1,2,3,4,5].splice(0,2) will return a new array containing the removed elements: [1,2]

You can then append this spliced array to the target array using .concat(), as in:

var selectedElements = [1,2,3,4,5].splice(0,2);
var targetArray = ['a', 'b', 'c'].concat(selectedElements);

This would return the targetArray with the selected elements attached to it, ie:

targetArray = ['a', 'b', 'c', 1, 2]

Danny Delott
  • 6,756
  • 3
  • 33
  • 57
  • I think he was using that as an example . . . the last paragraph of his question makes it seem like he's looking for a generic way to solve this, not specifically how to move that one element. – talemyn May 26 '15 at 16:10
  • Thanks Danny, this is a great start. So yea, the elements will almost never be the last element (they'll be random). The most likely scenario would be having a separate array of id's that need to be moved from one array to another. Or, there will be another item in the array like `move: true` that means it needs to be moved to the other array. You can infer that there is only one destination for these items always, but the items that need to be moved are random. – bryan May 26 '15 at 16:11
  • Technically, arrays are also objects in JS. From what I gather, he has an object, and he is storing arrays as some of the properties of the object. @bryan: There is an .inArray() method you could use to scan the array for a particular value (whatever you're searching for). It'll return the index so you can manipulate it further. (move it, delete it, store it in another variable before moving it, etc..) – Fata1Err0r May 26 '15 at 16:11
  • @Fata1Err0r - `inArray` (or `indexOf`) won't work for objects . . . even if the data is the same, they would be two different instances of objects containing the same data, so they wouldn't ever "match". – talemyn May 26 '15 at 16:17
  • @talemyn Correct me if I'm wrong, as I'm still learning jQuery/JS, but you could simply store a function for this as one of the object's methods. Then you can call it on whatever instance of the object you're using, and simply pass whatever search variable you wish to it. – Fata1Err0r May 26 '15 at 16:28
  • You could write the function then add it to the `Object.prototype` as a method for all instances of objects to be able to use, yes. – Danny Delott May 26 '15 at 16:29
  • @DannyDelott Yea, I figured so. Using your answer, combined with the search() method, should get the result he is after also. Just find the index with search(), then use splice/concat on it to remove/add. – Fata1Err0r May 26 '15 at 16:35
  • It might be possible (I honestly have never looked into something like this), but I would never recommend updating `Object.prototype` (or any other built in JS object) unless you absolutely have to. On top of that, I'd question whether or not updating `Object.prototype` with a new method would really be an "easier' approach than looping the arrays and investigating each object. – talemyn May 26 '15 at 17:22
1

jsFiddle using pure javascript

// your object
var o = {
    "numbers": [
        {"id":"11111"},
        {"id":"22222"}
    ],
    "letters": [
        {"id":"aaaaa"},
        {"id":"bbbbb"},
        {"id":"33333"}
    ]
};

// gets the index
// a = the array to look in
// p = the property to check
// v = the value you are looking for
function getIndex(a, p, v) {
    for(var i=0; i < a.length; i++) {
        if(a[i][p] == v) {
            return i;
        }
    }
}

// get the index from the letters array with property 'id' with value '33333'
var index = getIndex(o['letters'], 'id', '33333');
console.log(index);
// if you found the item
if (index !== undefined) {
    // get the actual item from the array
    var item = o['letters'][index];
    console.log(o['letters']);
    console.log(o['numbers']);
    console.log(item);
    // add the item to the other numbers array
    o['numbers'].push(item);
    // remove the item from the letters array
    o['letters'].splice(index, 1);
    console.log(o['letters']);
    console.log(o['numbers']);
} else {
    console.log('did not find the item');
}

using jQuery

Find object by id in array of javascript objects from there simply use the same javascript code to remove from one array and push (add) into the other

Community
  • 1
  • 1
abc123
  • 17,855
  • 7
  • 52
  • 82