3

I am generally new to JavaScript programming and front-end in generally so please don't judge me :), today I run into some tough problem and I don't how to solve so any help would be welcome. So here is the problem:

I have some JSON structure like on the JSON Example and it is normal javascript JSON, what I want to do is a format and switch elements in the array to the specific order, for example, let's say I have given this order of elements:

var jsonObject =[{"id":1,"name":"Marco0",
                  "age":0,"startDate":"1990-12-01T00:00:00",
                  "salary":333.2132,"currentRate":0.3}];

var order=["name","age","id","startDate","salary","currentRate"]

And i want to have something like this

var result=["Marco0","0","1","1990-12-01T00:00:00,....]

so the result corresponds to the order that was asked for and to be an array of value. I have also check this and that is the only partial answer.

2
  • Look at a snippet of jsonObject and the image. Commented Sep 17, 2018 at 16:17
  • There is no such thing as a JSON Object. JSON is always a string. Commented Sep 17, 2018 at 16:18

1 Answer 1

2

While having an array, you could map the array and map the wanted keys/values for a new array.

var object = [{ id: 1, name: "Marco0", age: 0, startDate: "1990-12-01T00:00:00", salary: 333.2132, currentRate: 0.3 }],
    order = ["name", "age", "id", "startDate", "salary", "currentRate"],
    result = object.map(o => order.map(k => o[k]));
    
console.log(result);

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

2 Comments

Yep, this works like charm tnx Nina, Vielen Dank!! Can you recommend me good book for JS
thanks, sorry no book advice, but checking questions and answerd could help a bit.

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.