1

I'm trying to manipulate this array and play with parse, push and save data commands. but for some reason is not reading. Any help? I'm super new to it and just trying to practice. Should be an array of objects.

    var employeeString = [
    { first_name: 'Josh', last_name:'Neil', nationality:'Indian'},
    { first_name: 'Brian', last_name:'Cok', nationality:'Canadian'},
    { first_name: 'Katja', last_name:'Fanta', nationality:'German'} 
];
    var jsonValues = JSON.stringify(employeeString);
    current_values = JSON.parse(jsonValues);

    var nationality = [];
    //console.log (current_values);

    for ( i = 0; i < current_values.length; i++) {
        var currentItem = current_values[i]
        var first_name = currentItem['first_name'];
        var last_name = currentItem['last_name'];

        //push value into array
        nationality.push(currentItem['nationality']);
        if (currentItem == 'first_name' || currentItem == 'last_name') { 
            return id[i+1]; 
        }
    }

        console.log (first_name , last_name);
3
  • 2
    you need no parse, because you have allready an array. Commented Sep 30, 2018 at 14:59
  • Im doing it already, have a look Commented Sep 30, 2018 at 15:02
  • please add what you like to get (the final result). Commented Sep 30, 2018 at 15:04

4 Answers 4

1

You don't need to parse the already JSON object. For example JSON.parse('[{x:10},{y:20}]'); will return JSON object from the string. This step is required to access the value of x and y, which are in string form as you can see '' around it. But in your case, you already got a JSON Object you don't need to parse it again. Further in console.log if you want to print first_name, do not add '' around it. Here is your working code

var current_values = [
  { first_name: 'Josh', last_name: 'Neil', Nationality: 'Indian' },
  { first_name: 'Brian', last_name: 'Cok', Nationality: 'Canadian' },
  { first_name: 'Katja', last_name: 'Fanta', Nationality: 'German' }
];

//     current_values = JSON.parse(employeeString);

//     console.log(current_values);

for (i = 0; i < current_values.length; i++) {
  var currentItem = current_values[i]
  var first_name = currentItem['first_name'];
  var last_name = currentItem['last_name'];
  console.log(first_name, last_name);
}

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

9 Comments

I got until this point now, but only prints the last name! if (currentItem == 'first_name') { // return obj[i+1];
currentItem contains the whole object { first_name: 'Josh', last_name: 'Neil', Nationality: 'Indian' }. what condition do you want to meet.
sum the names on key "first_name" and print all names. same for another keys.
sorry, I don't get "sum the names", you want to concatenate the names?
share the out put you want.
|
0

The JSON.parse method accepts a string, but you're giving it an array of objects.

var values = JSON.parse("[{'first_name': 'Josh', ...}, ...]");

Would work better.

In general, parsing will convert a string to objects. http://www.willamette.edu/~fruehr/haskell/seuss.html

Comments

0

The data type for employeeString is Array, but JSON.parse() is supposed to parse JSON data. So, you can first change employeeString into a JSON data before parse it.

3 Comments

var jsonValues = JSON.stringify(employeeString); current_values = JSON.parse(jsonValues); right?
@paulo.gomes Yes, this is one way to convert a non-JSON data to JSON data in JavaScript.
@paulo.gomes But you also realise that it doesn't do some useful thing. I mean if you have an array, just iterate it, no need to play with JSON. But if your original data is JSON (actually is string), then you just simply JSON.parse(yourData) and work with it.
0

You don't need to JSON.parse(employeeString), since it is already an object.


JSON.parse is used to create objects from their string representation, e.g. parse the string '{"prop":42}' to an actual object with a property prop and its value being 42. Since employeeString in your code is already an object, you don't need to JSON.parse it again.

BTW, you probably meant this:

// notice that this is now an actual string
var employeeString = `[
    { first_name: 'Josh', last_name:'Neil', Nationality:'Indian'},
    { first_name: 'Brian', last_name:'Cok', Nationality:'Canadian'},
    { first_name: 'Katja', last_name:'Fanta', Nationality:'German'} 
]`;

2 Comments

I'm trying to save all datas by "key" and print it. still not working
Also notice that everything after return statement is ignored.

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.