简体   繁体   English

JS:从对象数组中提取数据

[英]JS: Extracting data from an array of objects

I have a complex query with 100s of fields and nested fields.我有一个包含 100 个字段和嵌套字段的复杂查询。 What I want to do is, for each Index, extract the English and French text.我想要做的是,对于每个索引,提取英文和法文文本。 As you can see in the array, there is no French text for some indexes.正如您在数组中看到的,某些索引没有法语文本。 In that case I want to get the English text.在那种情况下,我想获得英文文本。 For me extracting the English text works fine because the text is already there, but incase of French, I get undefined errors.对我来说,提取英文文本工作正常,因为文本已经存在,但是如果是法语,我会收到未定义的错误。 What would be the best way to implement this.实现这一点的最佳方法是什么。 Is Loadash needed for this or just pure JS methods?这个方法需要 Loadash 还是纯 JS 方法?

Just to be clear, I have erros with extracting french because in some fields, french text is not available, I want to use the english value in that case.为了清楚起见,我在提取法语时出错,因为在某些领域,法语文本不可用,在这种情况下我想使用英语值。

Also It is recommend if I am able to get the English and French values by it's language field rather than the index.此外,如果我能够通过它的语言字段而不是索引来获取英语和法语值,则建议这样做。 I have no idea how to do that.我不知道该怎么做。

Any suggestion, documentation is appreciated.任何建议,文档表示赞赏。 Thank you!谢谢!

example array:示例数组:

[
 {
    id: "1",
    name: [
      {
        language: "en-US",
        text: "HOLIDAY"
      }
    ],
    order: 6,
    Groups: [
      {
        name: [
          {
            language: "en-US",
            text: "REGULAR"
          }
        ],
        code: "REGEARN"
      },
      {
        name: [
          {
            language: "en-US",
            text: "CHARGE"
          }
        ],
        code: "CHARGE"
      }
    ]
  }
]

and here is the code sandbox that reproduces my error: CODE SAND BOX https://codesandbox.io/s/javascript-forked-5073j这是重现我的错误的代码沙箱: CODE SAND BOX https://codesandbox.io/s/javascript-forked-5073j

EDIT:编辑:

EXPECTED OUTPUT:预期产出:

{
  key: key,
  englishtext: "Value Here",
  frenchtext: "Value Here"
}

below is a working code, but issue is it does not work when there is no french language or that field.下面是一个工作代码,但问题是当没有法语或该字段时它不起作用。 I get undefined errors.我收到未定义的错误。 So is it possible I can get the needed data from the language field?那么我是否有可能从语言领域获得所需的数据?

x.map((y) => ({
    key: y.id,
    name: y.name[0].text,
    groupname: y.Groups ? x.Groups[0].name?.[0].text : 'N/A',
  }))

Do you expect result like this?你期待这样的结果吗? If you don't mind lodash.如果你不介意 lodash。

const _ = require('lodash');

const getNames = (arr) => {
  return arr.map((obj) => {
    const id = obj.id;
    const englishtext = _.get(obj, 'name[0].text', 'N/A');
    const frenchtext = _.get(obj, 'name[1].text', englishtext);

    return { id, englishtext, frenchtext };
  });
};
        
console.log(getNames(x));
// [
//   { id: '1', englishtext: 'HOLIDAY', frenchtext: 'HOLIDAY' },
//   { id: '2', englishtext: 'Stat Holiday', frenchtext: 'Congé Férié' },
//   { id: '3', englishtext: 'Over', frenchtext: 'Over' }
// ] 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM