1

sorry I just learned the app script and my english is not good. on the Google app script, based on the example of json array, how do I get itemA and itemC output?

{
    "items":[
        {
            "itemA":123,
            "itemB":"qwe",
            "itemC":"asd"
        },
        {
            "itemA":456,
            "itemB":"rty",
            "itemC":"fgh"
        },
        {
            "itemA":789,
            "itemB":"uio",
            "itemC":"jkl"
        }
    ]
}

I want the output like this :

123, asd
456, fgh
789, jkl

I really appreciate all your help.

2 Answers 2

3

This script gives the output you want.
Happy?

var json = {
  "items": [{
      "itemA": 123,
      "itemB": "qwe",
      "itemC": "asd"
    },
    {
      "itemA": 456,
      "itemB": "rty",
      "itemC": "fgh"
    },
    {
      "itemA": 789,
      "itemB": "uio",
      "itemC": "jkl"
    }
  ]
}
var output_text = "";
json.items.forEach(function(item) {
  output_text += item.itemA + ", " + item.itemC + "\n";
});
console.log(output_text);

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

2 Comments

delete deletes in-place. If the intent is modifying the original object, forEach is better suited for the iteration.
@Antoni, thank you for your answer, please check update of my question
0

Try the following code:

let obj = {
    "items":[
        {
            "itemA":123,
            "itemB":"qwe",
            "itemC":"asd"
        },
        {
            "itemA":456,
            "itemB":"rty",
            "itemC":"fgh"
        },
        {
            "itemA":789,
            "itemB":"uio",
            "itemC":"jkl"
        }
    ]
};

function genOutput() {
  var output = obj.items.map( item => [item.itemA,item.itemC] );
  console.log( output );
};

Run function genOutput, you will get the following output:

[ [ 123, 'asd' ], [ 456, 'fgh' ], [ 789, 'jkl' ] ]

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.