3

I have a object.

var dl_items;

After a loop for inputing data:

dl_items[code] = itemObject;

I have a array:

dl_items : {
            "code_A" : { "index" : 1, "status" : 2, "name" : A_data},
            "code_B" : { "index" : 2, "status" : 0, "name" : B_data},
            "code_C" : { "index" : 3, "status" : 1, "name" : C_data},
            "code_D" : { "index" : 4, "status" : 2, "name" : D_data},
            "code_E" : { "index" : 5, "status" : 4, "name" : E_data}
           }

Now I want to remove "dl_items[code_D]" and insert it into after "code_A" (index 2) for result like :

 dl_items : {
            "code_A" : { "index" : 1, "status" : 2, "name" : A_data},
            "code_D" : { "index" : 4, "status" : 2, "name" : D_data},
            "code_B" : { "index" : 2, "status" : 0, "name" : B_data},
            "code_C" : { "index" : 3, "status" : 1, "name" : C_data},                
            "code_E" : { "index" : 5, "status" : 4, "name" : E_data}
            }

I try to use "delete" after using a loop to find index of code_D:

delete dl_items[code_D];

and it successful removed but how can i insert code_D into his new index ?

Edit : Thanks all everyone to help me understand more about array.

Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317
Zero-IT
  • 33
  • 6
  • 1
    basically you show an object with objects. properties in objects have actually no order. you could use an array for ordered items. – Nina Scholz Dec 02 '16 at 10:50
  • 2
    That's neither [JSON](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) nor an [array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). `dl_items` is an [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) – Andreas Dec 02 '16 at 10:51
  • Please correct your array – Amoolya S Kumar Dec 02 '16 at 10:51
  • as @NinaScholz said, you can use an array, that will hold objects, that way you can easily just rewrite the index you want `array[3] = { status: 2, name: d_data}`, in javascript array is an object. – Yan Mayatskiy Dec 02 '16 at 10:52
  • 1
    This has nothing to do with JSON. JSON is a way of encoding data in strings. – RemcoGerlich Dec 02 '16 at 11:01
  • In JS, do not think in terms of "removing" or "inserting" elements into arrays. Instead, think in terms of creating a new array, with some elements added or removed. –  Dec 02 '16 at 11:06

3 Answers3

1

Since object doesn't have an order, you need to convert your current implementation into array:

var dl_items = [];

When you need to add an item to the array:

dl_items.push({ code: code, item: itemObject });

Now, the similar data as array from your question is:

dl_items: [
            { code :"code_A", item: { index: 1, status: 2, name: "A_data" } },
            { code :"code_B", item: { index: 2, status: 0, name: "B_data" } },
            { code :"code_C", item: { index: 3, status: 1, name: "C_data" } },
            { code :"code_D", item: { index: 4, status: 2, name: "D_data" } },
            { code :"code_E", item: { index: 5, status: 3, name: "E_data" } },
          ]

In order to move the entry with code_D after the entry with code_A, use the following:

var codeDEntry = dl_items[3];
dl_items = dl_items
    .filter(function(entry) {
        return entry !== codeDEntry;
    })
    .splice(1, 0, codeDEntry);

Hope this helps!

Naor
  • 23,465
  • 48
  • 152
  • 268
  • Can i ask why you used `unshift` ? In w3school, it said that `unshift` using to add a item to beginning of array. And i think your new array will be : D, A, B, C , E ? Please correct me if i was wrong :D – Zero-IT Dec 02 '16 at 11:26
  • Sorry, you completely right. I didn't read the question properly. I Fixed my answer. – Naor Dec 02 '16 at 11:30
  • Thank you. Now i will refactor the structure of my code a lot :D – Zero-IT Dec 02 '16 at 11:33
  • Glad I could help. – Naor Dec 02 '16 at 11:52
0

You can make a temp var like this :

tempItem = dl_items.code_D;
dl_items.code_D = dl_items.code_B;
dl_items.code_B = tempItem;
HichamEch
  • 635
  • 1
  • 10
  • 18
0

What you have here is an object, not an array. Therefore, there is no concept of an index here. You can map your object keys into an array as follows:

 let array = Object.keys(dl_items);

You can then reorder their positions.

Amoolya S Kumar
  • 1,458
  • 9
  • 10