简体   繁体   English

我如何从嵌套数组中获取数据并得到结果

[英]how i can get data from nested array and get the result

const source = [
    {
      id: "1",
      data: {
          first_name: "Rian",
          last_name: "Nugraha"
      },
      school: {
        id: "1",
        data: "PLMK-JKT"
      },
    },
    {
      id: "2",
      full_name: "Ari Santoso",
      school: {
        id: "1",
        short_name: "GRSR",
        data: "JKT"
      },
    },
    {
      id: "3",
      data: {
        first_name: "Rahman",
        last_name: "Sunggara"
    },
    school: {
        id: "1",
        short_name: "GELM",
        data: "JKT"
      },
    },
    {
        id: "4",
        data: {
            first_name: "Rian",
            last_name: "Nugraha"
        },
        school: {
            id: "2",
            data: "PLMK-BDG"
        },
    },
  ]

so, I want to combine first_name and last_name into fullname of the nested array or combine short_name and data in school nested array but always undefined, the result like below:所以,我想将 first_name 和 last_name 组合成嵌套数组的全名,或者在学校嵌套数组中组合 short_name 和数据,但总是未定义,结果如下:

{ name: 'Rian Nugraha', school_name: 'PLMK-JKT } {名称:'Rian Nugraha',学校名称:'PLMK-JKT}

Here how to access array @ element (0) and combine first and last names这里如何访问数组@元素(0)并结合名字和姓氏

var fullName = source[0].data.first_name + " " + source[0].data.last_name;

To add school:添加学校:

var fullNameAndSchool = source[0].data.first_name + " " + source[0].data.last_name + " is from school " + source[0].school.data;

Did using a ternary operator是否使用了三元运算符

 const source = [ { id: "1", data: { first_name: "Rian", last_name: "Nugraha" }, school: { id: "1", data: "PLMK-JKT" }, }, { id: "2", full_name: "Ari Santoso", school: { id: "1", short_name: "GRSR", data: "JKT" }, }, { id: "3", data: { first_name: "Rahman", last_name: "Sunggara" }, school: { id: "1", short_name: "GELM", data: "JKT" }, }, { id: "4", data: { first_name: "Rian", last_name: "Nugraha" }, school: { id: "2", data: "PLMK-BDG" }, }, ] const resArray = source.map((el) => { let name = el.data? (el.data.first_name+" "+el.data.last_name): el.full_name; let school_name = el.school.short_name? (el.school.short_name+" "+el.school.data): el.school.data; const result = { name: name, school_name: school_name } return result; }) console.log(resArray)

const result = source.map(({data, school, full_name}) => { return {name: (typeof data) === "undefined" ? full_name : `${data.first_name + " "+ data.last_name}`, school_name: school.hasOwnProperty("short_name") ? `${school.short_name + " "+ school.data}` : school.data}; }); console.log(result); /*[ { name: 'Rian Nugraha', school_name: 'PLMK-JKT' }, { name: 'Ari Santoso', school_name: 'GRSR JKT' }, { name: 'Rahman Sunggara', school_name: 'GELM JKT' }, { name: 'Rian Nugraha', school_name: 'PLMK-BDG' } ]*/

This should help provide an example of how to get what you need.这应该有助于提供如何获得所需内容的示例。

const mapfunc = o => ({
  name: `${o.data.first_name} ${o.data.last_name}`,
  school_name: o.school.data
});

source.map(mapfunc);

See documentation for Array.prototype.map and string templates.请参阅 Array.prototype.map 和字符串模板的文档。

if you need first name and last name you can use map method to check each array object and get the specified value using if conditions..如果您需要名字和姓氏,您可以使用 map 方法检查每个数组 object 并使用 if 条件获取指定的值。

 const source = [ { id: "1", data: { first_name: "Rian", last_name: "Nugraha" }, school: { id: "1", data: "PLMK-JKT" }, }, { id: "2", full_name: "Ari Santoso", school: { id: "1", short_name: "GRSR", data: "JKT" }, }, { id: "3", data: { first_name: "Rahman", last_name: "Sunggara" }, school: { id: "1", short_name: "GELM", data: "JKT" }, }, { id: "4", data: { first_name: "Rian", last_name: "Nugraha" }, school: { id: "2", data: "PLMK-BDG" }, }, ] source.map((sources) =>{ if(sources.data){ if(sources.data.first_name && sources.data.last_name ) { console.log(sources.data.first_name+" "+sources.data.last_name) } } } )

 const source = [ { id: "1", data: { first_name: "Rian", last_name: "Nugraha" }, school: { id: "1", data: "PLMK-JKT" }, }, { id: "2", full_name: "Ari Santoso", school: { id: "1", short_name: "GRSR", data: "JKT" }, }, { id: "3", data: { first_name: "Rahman", last_name: "Sunggara" }, school: { id: "1", short_name: "GELM", data: "JKT" }, }, { id: "4", data: { first_name: "Rian", last_name: "Nugraha" }, school: { id: "2", data: "PLMK-BDG" }, }, ] for (var i=0; i<=source.length; i++){ if (source[i]===source[0]){ console.log("name:" + source[i].data.first_name +" "+source[i].data.last_name + ", school_name: " + source[i].school.data); }if(source[i]===source[1]){ console.log("name: " + source[1].full_name + ", school_name: " + source[1].school.short_name + "-" + source[1].school.data); }if(source[i]===source[2]){ console.log("name:" + source[i].data.first_name + " "+source[i].data.last_name + ", school_name: " + source[i].school.short_name + "-" + source[i].school.data); }if(source[i]==source[3]){ console.log("name:" + source[i].data.first_name +" "+source[i].data.last_name + ", school_name: " + source[i].school.data); } }

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

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