0

I need to convert following json array

{
    "employees": [{
        "firstName": "John",
        "lastName": "Doe"
    }, {
        "firstName": "Anna",
        "lastName": "Smith"
    }, {
        "firstName": "Peter",
        "lastName": "Jones"
    }]
}

to a json array without name 'Employee'

 {[
    {"firstName":"John", "lastName":"Doe"},
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]}

using javascript

2
  • 1
    Now the format of the resulting object is not valid Commented Sep 8, 2015 at 4:51
  • 1
    obj.employees ; no transform or iteration needed. Commented Sep 8, 2015 at 4:57

2 Answers 2

1

try this one

var employeeArr =  {"employees":[
         {"firstName":"John", "lastName":"Doe"},
         {"firstName":"Anna", "lastName":"Smith"},
         {"firstName":"Peter", "lastName":"Jones"}
     ]}

var emp = $.each(employeeArr.employees, function(index , value){
                  return value;
            },{})

console.log(emp);
Sign up to request clarification or add additional context in comments.

4 Comments

it works, but why do all that for no reason whatsoever?
@dandavis: my english is not so good ?? what are you asking i didnt understand in comment area Sorry for that
var emp=employeeArr.employees; gives the same as your each() routine... ;) i can write x=(2+3) instead of x=5, but why?
these are different approches :) i have this one so .. :)
1

Simply can do ,

var arr ={"employees":[
         {"firstName":"John", "lastName":"Doe"},
         {"firstName":"Anna", "lastName":"Smith"},
         {"firstName":"Peter", "lastName":"Jones"}
         ]};
var employee = arr.employees;
console.log(employee);

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.