添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
光明磊落的铁板烧  ·  Need help with ES6: ...·  3 周前    · 
追风的小虾米  ·  prefer-destructuring ...·  3 周前    · 
果断的移动电源  ·  React Hooks: Array ...·  3 周前    · 
酒量大的洋葱  ·  鲁晓艳·  2 月前    · 
八块腹肌的生菜  ·  Streaming Messages - ...·  3 月前    · 
叛逆的烈酒  ·  No trusted ...·  3 月前    · 
Community Tip - You can change your system assigned username to something more personal in your community settings .

Hi all,

I am making a GET API call from a ThingWorx service. I want to parse the JSON data from the API call and search through it for certain values, which will then be pushed into an array. It seems to me that standard JavaScript methods like .filter() and .find() don't work in ThingWorx.

Can this be done in the same service or will I need multiple?

Any help would be appreciated.

Hello,

You can find some information here .

Thingworx does support arrow functions, so if you are comfortable using those you can go ahead with that.

If your data looks similar to this:

{array:[
{key:value},{key:value},{key:value},{key:value}
]}

Then you can use the forEach() function to loop through the objects in your array, something like this:

array.forEach(object => {
// do your filtering here and array push here
});

Hope this helps,

Regards,

Jens

Hello,

You can find some information here .

Thingworx does support arrow functions, so if you are comfortable using those you can go ahead with that.

If your data looks similar to this:

{array:[
{key:value},{key:value},{key:value},{key:value}
]}

Then you can use the forEach() function to loop through the objects in your array, something like this:

array.forEach(object => {
// do your filtering here and array push here
});

Hope this helps,

Regards,

Jens

Hello,

Services like find and filter work in ThingWorx, including ContentLoaderFunctions calls. This example works for me:

let json = Resources["ContentLoaderFunctions"].GetJSON({
	url: 'https://datausa.io/api/data?drilldowns=Nation&measures=Population'
let result = {	// JSON
    'filtered': json.data.filter(row => (row['ID Year'] < 2015))

The only related non-intuitive thing I know is that ThingWorx doesn't allow plain arrays as JSON objects and will always wrap them in "array" element, i.e.:

// Service return type is JSON
let result = [1, 2, 3]; // Will actually return {"array": [1, 2, 3]}

/ Constantine

Hello Constantine,

 

Whenever I try and run a service that uses .filter(), I receive this error:

Error executing service Get_IR_APIs. Message :: TypeError: Cannot call method "filter" of undefined - See Script Error Log for more details.

It is also worth mentioning, while searching for the JSON objects in the array of JSON objects, I want to search for values using the value part, of the key and value pair structure.

 

Thank you