简体   繁体   中英

Build array dynamically with JavaScript/Postman

Looking to create a JSON response body with JavaScript/Postman as a pre-request script with some dynamic variables. The issue I'm having is dynamically generating arrays inside the array. The data I'm receving is a CSV file so the value is like so Value1|Value2 and I need to separate the values from the pipe separator and pushing them into their own category array

Using the CSV value from the above example the output should be like this:

{
"id": 1,
"store": 1,
"title": custom_title,
"url": custom_url,
"content": content,
"categories":[
    {
       "id":1,
       "name":"Value1",
       "type":"Type"
     },
     {
       "id":1,
       "name":"Value2",
       "type":"Type"
     },
    //add more if needed dynamically
]
}

I tried something like this

var catNum = "Blog|Healthy".split('|');
catNum.forEach((value, name) => bodyRequest.categories.push({ 
    "id": 1,
    "type": "Category",
    "name": value.toLowerCase(),
    "blog_category_path": value
}));

Trying to push the above into the categories array "categories":[]

var catNum = "Blog|Healthy".split('|');

returns an array strings to catNum.

Do this rather

catNum.forEach(value => bodyRequest.categories.push({ 
    "id": 1,
    "type": "Category",
    "name": value.toLowerCase(),
    "blog_category_path": value
}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM