JavaScript - 将一个对象的数组列表转换成新的格式化形式?

3
我正在尝试使用JavaScript将一个包含数组的对象转换为另一个对象。以下是对象字段的示例以及格式化后的对象应该是什么样子的。
let Fields = {
  GAME: [
    { code: '{{PES}}', title: { en: "playPES"} },
    { code: '{{FIFA}}', title: { en: "playFIFA " } },
  ]
};

我需要新字段看起来像这样。
let newFields = {
name: 'GAME', 
tags:[
   { name: 'playPES', value: "{{PES}}" },
   { name: 'playFIFA', value: "{{FIFA}}" }
      ]},
 

有位贡献者向我提出了这样的方法,但我认为其中某些需要修改,但却想不出来如何修改。

export const transform = (fields) => ({
  tags: Object .entries (fields) .map (([name, innerFields]) => ({
    name,
    tags: innerFields.map(({code, title: title: {en})=>({name: en, value: code}))
  }))
});

// newFields = transform(Fields)

我刚开始接触 JavaScript,非常感谢任何帮助,谢谢。


这里的信息不够充分。你是否在任何地方导入并调用了 transform(Fields)?有没有抛出任何错误?发生了什么? - charlietfl
是的,我称它为newFields = transform(Fields),但它没有将fields转换为那种格式,函数中有些问题。 - James Bertaha
好的,这比仅仅说“它不起作用”要好一些的解释。始终尝试包括任何调试详细信息。 - charlietfl
6个回答

2

const transform = (o) => {
    return Object.entries(o).map((e)=>({
        name: e[0],
        tags: e[1].map((k)=>({name: (k.title)?k.title.en:undefined, value: k.code}))
    }))[0]
}

console.log(transform({
  GAME: [
    { code: '{{PES}}', title: { en: "playPES"} },
    { code: '{{FIFA}}', title: { en: "playFIFA " } },
  ]
}))


0

let Fields = {
    GAME: [
      { code: '{{PES}}', title: { en: "playPES"} },
      { code: '{{FIFA}}', title: { en: "playFIFA " } },
    ]
  };
  let newFields = {
    name: 'GAME', 
        tags:[
            { name: 'playPES', value: "{{PES}}" },
            { name: 'playFIFA', value: "{{FIFA}}" }
        ]
    }
  let answer = {
    name: "Game",
        tags: [

        ]
  }
  Fields.GAME.map(i => {
      var JSON = {
        "name": i.title.en,
        "value": i.code
      }
      answer.tags.push(JSON);
  });
  console.log(answer);


0

我认为这样更易读,但不一定更容易... 如果你想要结果作为对象,你需要使用reduce,因为当你这样做时

Object.keys(Fields)

你的对象转换为数组,但是 reduce 可以将数组转换回对象。

let Fields = {
  GAME: [
    { code: '{{PES}}', title: { en: "playPES"} },
    { code: '{{FIFA}}', title: { en: "playFIFA " } },
  ]
};

const result = Object.keys(Fields).reduce((acc, rec) => {
    return {
      name: rec,  
      tags: Fields[rec].map(el => {
        return {
          name: el.title.en,
          value: el.code
        }
    })
    }
}, {})

console.log(result)


0

使用您发布的entries方法:

let Fields = {
  GAME: [
    { code: '{{PES}}', title: { en: "playPES"} },
    { code: '{{FIFA}}', title: { en: "playFIFA " } },
  ]
};

// 1. Obtain keys and values from first object
Fields = Object.entries(oldFields);

// 2. Create new object
const newFields = {};

// 3. Create the name key value pair from new Fields array
newFields.name = Fields[0][0];

// 4. Create the tags key value pair by mapping the subarray in the new Fields array
newFields.tags = Fields[0][1].map(entry => ({ name: entry.title.en, value: entry.code }));

0

Object.entries(Fields) 将返回以下内容:

[
  "GAME",
  [TagsArray]
]

Object.entries(Fields).map将映射这些值。

第一个映射只会接收GAME,而不是数组。

将代码更改为以下内容:

export const transform = (Fields) => {
  const [name, tags] = Object.entries(Fields);

  return {
    name,
    tags: tags.map(({ code, title }) => ({
      name: title.en,
      value: code
    }))
  }
}

希望能有所帮助 :)

0
    let Fields = {
    GAME: [
      { code: '{{PES}}', title: { en: "playPES"} },
      { code: '{{FIFA}}', title: { en: "playFIFA " } },
    ]
};

const transform = (fields) => ({
    tags: Object .entries (fields) .map (([name, innerFields]) => ({
      name,
      tags: innerFields.map(({code, title: title,en})=>({name: title.en, value: code}))
    }))
  });
//check required output in console
console.log(transform(Fields));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接