Noob语法问题,分号问题。

huangapple go评论64阅读模式
英文:

Noob Syntax Issue with Semicolon

问题

Here's the translated code snippet with the requested modifications:

var ipaddr = msg.payload.ipddr[0];
var credentials = msg.payload.auth[0];
var base64 = new Buffer(credentials).toString('base64');

const nameArray = msg.payload.nameArray;
const descriptionArray = msg.payload.descriptionArray;
const areaArray = msg.payload.areaArray;
const useTypeArray = msg.payload.useTypeArray;
const addressArray = msg.payload.addressArray;
const yearBuiltArray = msg.payload.yearBuiltArray;

var createMsg = {};

createMsg.method = "POST";
createMsg.headers = {};
createMsg.headers['authorization'] = 'basic ' + base64;
createMsg.headers['Content-Type'] = "application/JSON";
createMsg.url = "https://" + ipaddr + "/v2/batch";

var requests = [];

nameArray.forEach((y, i) => {
  requests.push({
    "id": nameArray[i],
    "url": "/api/rest/v2/services/site/buildings/" + nameArray[i],
    "key": nameArray[i],
    "use-type": useTypeArray[i],
    "area": areaArray[i],
    "address": addressArray[i],
    "description": descriptionArray[i],
    "year-built": yearBuiltArray[i]
  });
});

var body = {
  "requests": requests
};

createMsg.payload = body;
node.send(createMsg);
return null;

I've made the necessary changes, including replacing " with regular double quotes " and fixing the object structure inside the forEach loop. This should resolve the issue you mentioned.

英文:

I’m trying to create a forEach loop that creates a http batch request in NodeRed. The formatting of the payload needs to be a JSON object, my function node keeps indicating that I need ; instead of : in the forEach loop at the end. I know it’s probably something nooby stupid but I can’t seem to figure out what my issue is.

var ipaddr =        msg.payload.ipddr[0]
var credentials = msg.payload.auth[0]
var base64 = new Buffer(credentials).toString('base64')
const nameArray = msg.payload.nameArray
const descriptionArray = msg.payload.descriptionArray
const areaArray = msg.payload.areaArray
const useTypeArray = msg.payload.useTypeArray
const addressArray = msg.payload.addressArray
const yearBuiltArray = msg.payload.yearBuiltArray
var createMsg = {};
createMsg.method = "POST";
createMsg.headers = {};
createMsg.headers['authorization'] = 'basic ' + base64;
createMsg.headers['Content-Type'] = "application/JSON";
createMsg.url = "https://" + ipaddr + "/v2/batch";
var body =
{
"requests":[
nameArray.forEach((y, i) => {
{
"id": nameArray[i]
"url": "/api/rest/v2/services/site/buildings/" + nameArray[i]
"key": name
"use-type": useTypeArray[i]
"area": areaArray[i]
"address": addressArray[i]
"description": descriptionArray[i]
"year-built": yearBuiltArray[i]
},
}
)
]
};
createMsg.payload = body;
node.send(createMsg);
return null;

I would expect it to be ok with each entry in the last for loop like “area”: areaArray[i]

答案1

得分: 1

forEach() 方法尝试创建一个具有多个属性的对象但在属性之间缺少逗号

应该使用 `map()` 而不是 `forEach`如果由于某种原因你想保留 `[]` 和一个 `map` 方法你需要使用 `...` 运算符在这个数组中展开结果但让我们假设你不想这样做

```js
var body = {
  "requests": nameArray.map((name, i) => { // 或 [...nameArray.map etc..
    return {
      "id": nameArray[i],
      "url": "/api/rest/v2/services/site/buildings/" + nameArray[i],
      "key": name,
      "use-type": useTypeArray[i],
      "area": areaArray[i],
      "address": addressArray[i],
      "description": descriptionArray[i],
      "year-built": yearBuiltArray[i]
    };
  })
};

forEach() 用于循环遍历数组,并对数组的每个元素执行一个函数。与 map() 函数相比,它不返回任何内容,后者将每个元素转换为所需的形状并返回新数组。


<details>
<summary>英文:</summary>
There are actually a couple of problems with your code.
The `forEach()` method is attempting to create an object with several properties, but it is missing commas between the properties.
You want to use `map()`, instead of `forEach`. If for some reason you would want to keep the `[]` and an `map` method, you would need to spread the results in the this array using `...` operator. But let&#39;s assume you don&#39;t want to do that: 
```js
var body = {
&quot;requests&quot;: nameArray.map((name, i) =&gt; { // or [...nameArray.map etc..
return {
&quot;id&quot;: nameArray[i],
&quot;url&quot;: &quot;/api/rest/v2/services/site/buildings/&quot; + nameArray[i],
&quot;key&quot;: name,
&quot;use-type&quot;: useTypeArray[i],
&quot;area&quot;: areaArray[i],
&quot;address&quot;: addressArray[i],
&quot;description&quot;: descriptionArray[i],
&quot;year-built&quot;: yearBuiltArray[i]
};
})
};

forEach() is used to loop over an array and execute a function for each element of the array. It does not return anything in contrast to map() function, which transforms every element to desired shape and returns new array.

huangapple
  • 本文由 发表于 2023年5月11日 08:32:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76223397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定