使用map失败转换属性得到意外的令牌错误

时间:2018-03-01 04:59:41

标签: javascript ecmascript-6

我有嵌套的对象结构数组看起来像这样

first()

我使用ES6来截断名称属性,基于' en':

const resp = [
  [
    {
      id: 1
      "name": {
        "en": {
          "language": "en",
          "value": "something"
        },
        "id": {
          "language": "th",
          "value": "something else"
        }
      }
    }
  ]
]

但是我收到了意外的令牌错误?

我想产生这个结果

resp = resp.map(o => ({
      ...o,
      o.map(o2 => ({ //unexpected token
        ...o2,
        name: o2.name.en.value
      })
    )
  })
)

3 个答案:

答案 0 :(得分:1)

.map上使用array,您只需要返回一个带有ID和名称的对象。

const resp = [
  [
    {
      id: 1,
      "name": {
        "en": {
          "language": "en",
          "value": "something"
        },
        "id": {
          "language": "th",
          "value": "something else"
        }
      }
    }
  ]
];

let result = resp[0].map(
  el => {return {"id": el.id, "name": el.name.en.value}}
);

console.log(result);

您可能需要将结果包装在另一个数组中。

答案 1 :(得分:1)



const resp = [
    [{
        id: 1,
        "name": {
            "en": {
                "language": "en",
                "value": "something"
            },
            "id": {
                "language": "th",
                "value": "something else"
            }
        }
    }]
];

const result = resp.map(o => {
    return o.map(o2 => {
        return {
            id: o2.id,
            name: o2.name.en.value
        }
    })
});

console.log(result);




答案 2 :(得分:1)

这也可以使用Array.reduce()函数来实现,而不是使用Array.map(),因为@AushGupta有答案。详细说明和答案位于MDN



const resp = [
          [
              {
                  id: 1,
                  "name": {
                      "en": {
                          "language": "en",
                          "value": "something"
                      },
                      "id": {
                          "language": "th",
                          "value": "something else"
                      }
                  }
              },
              {
                  id: 3,
                  "name": {
                      "en": {
                          "language": "en",
                          "value": "newthing"
                      },
                      "id": {
                          "language": "th",
                          "value": "something else"
                      }
                  }
              }
          ],
          [
              {
                  id: 2,
                  "name": {
                      "en": {
                          "language": "en",
                          "value": "something"
                      },
                      "id": {
                          "language": "th",
                          "value": "something else"
                      }
                  }
              }
          ]
      ];
	  var result = resp.reduce((results, current) => {
          var items = current.reduce((items, item) => {
              items.push({
                  id: item.id,
                  name: item.name.en.value
              });
              return items;
          }, []);
          results.push(items);
          return results;
      }, []);
      console.log(result);

<html>
   <body>
   </body>
</html>
&#13;
&#13;
&#13;