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.

6
  • 1
    basically you show an object with objects. properties in objects have actually no order. you could use an array for ordered items. Commented Dec 2, 2016 at 10:50
  • 2
    That's neither JSON nor an array. dl_items is an object Commented Dec 2, 2016 at 10:51
  • Please correct your array Commented Dec 2, 2016 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. Commented Dec 2, 2016 at 10:52
  • 1
    This has nothing to do with JSON. JSON is a way of encoding data in strings. Commented Dec 2, 2016 at 11:01

3 Answers 3

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!

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

4 Comments

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
Sorry, you completely right. I didn't read the question properly. I Fixed my answer.
Thank you. Now i will refactor the structure of my code a lot :D
Glad I could help.
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;

Comments

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.

Comments

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.