可以使用map()方法来将原数组转换为新的格式:

const deviceNames = ["TCL-CN-MT9653-C11G", "TCL-CN-T963-S11"];

const newDeviceNames = deviceNames.map((name, index) => ({
  id: (index + 1).toString(),
  title: name
}));

console.log(newDeviceNames);
// Output: [{id:'1',title:'TCL-CN-MT9653-C11G'},{id:'2',title:'TCL-CN-T963-S11'}]

在上面的代码中,我们使用map()方法对原数组进行遍历,将每个元素转换为一个新的对象,该对象包含idtitle两个属性。id属性的值为当前元素在数组中的索引加一,title属性的值为当前元素的值。最后,我们将转换后的新数组赋值给newDeviceNames变量。

const deviceNames= TCL-CN-MT9653-C11G TCL-CN-T963-S11用什么js方法把这个组成const deviceNames=id1titleTCL-CN-MT9653-C11Gid2titleTCL-CN-T963-S11

原文地址: https://www.cveoy.top/t/topic/ffdp 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录