Javascript React提取嵌套json对象

Javascript React提取嵌套json对象,javascript,reactjs,Javascript,Reactjs,如何从嵌套的json列表中提取“jobs”对象,如下所示: result: { person: [ { name: "" address: "" jobs: [ { company:"" }, { company:"" }

如何从嵌套的json列表中提取“jobs”对象,如下所示:

result:
{
 person:
  [
    {
       name: ""
       address: ""
       jobs: [
         {
           company:""
          },
          {
            company:""
           }
         ]
       }
      ]
     }

谢谢

编写一个提取对象属性的通用方法

函数onExtract(键、数据){
if(对象(数据)){
用于(让项目输入数据){
如果(键===项){
返回数据[项目];
}
const res=onExtract(键,数据[项]);
如果(res!==null)返回res;
}
}
if(iArray(数据)){
对于(数据项){
const res=onExtract(键,项);
如果(res!==null)返回res;
}
}
返回null;
}
功能对象(obj){
返回Object.prototype.toString.call(obj)=“[Object Object]”;
}
函数isArray(arr){
返回Object.prototype.toString.call(arr)=“[对象数组]”;
}
//试验
常数数据={
人:[
{
姓名:“,
地址:“,
工作:[
{
公司:“
},
{
公司:“
}
]
}
]
};
log(onExtract(“作业”,数据));

假设您有一个返回值var,其中包含此json值

let mappedCompanies = return.person.map(person => 
     person.jobs.map(job => job.company)
).flatMap(m => m)
MappedCompanys将包含一个数组,该数组包含“person”中每个寄存器的所有公司名称,所有这些名称都作为一个字符串数组


您可以在此处阅读有关Array.map()的更多信息:

查询
人员[]
和查找工作的动态方法是使用javascript
map()
方法

下面是没有注释的代码

  const personsJobs = (personName, personAddress) => {
    const jobs = result.person.map((el) => {
        if (el.name === personName && el.address === personAddress) {
          return el.jobs;
        } else {
          return null;
        }
      })
      .filter((el) => el !== null);

    return jobs;
  };

  console.log(personsJobs("wyatt", "1234 test ln"));
下面是带有注释的代码,用于解释
PersonJob
功能的工作原理


// Blow is an ES6 arrow function with the parameters 'personName' and 'personAddress',
// which represents the person in which you are querying for jobs (using both a persons 
// name and address so in the case of persons with the same name, you only find the jobs
// of the person you want).

  const personsJobs = (personName, personAddress) => {

// Since 'person' is an array, we can use the 'map' method as stated before, which 
// will create a new array (jobs) that will store the jobs a specific person has.

    const jobs = result.person.map((el) => {

// el stands for the current position in the person array. 
// if el's (the current person) name and address values are equal to that of the 
// parameters personName and personAddress, then that persons jobs are added to the jobs // array, however, if el does not satisfy the two parameters, null is added to the jobs
// array.
// The array, upon completion, will look something like this: ["programmer", null, null]

        if (el.name === personName && el.address === personAddress) {
          return el.jobs;
        } else {
          return null;
        }
      })

// Finally, the filter method is called to remove all null values so that you will
// only have the persons job in the jobs array.
// After filtering, the array will look like this: ["programmer"]

      .filter((el) => el !== null);

    return jobs;
  };

// Prints the array of wyatt's jobs

  console.log(personsJobs("wyatt", "1234 test ln"));


因此,在函数结束后,您将动态找到特定人员的工作。

您可以使用flatMap函数,如:


const-jobsData=result.person.flatMap(item=>item.jobs)这是一个灵活的解决方案,使用

//const objectScan=require('object-scan');
const data={person:[{name:'',address:'',jobs:[{company:'},{company:'}]};
log(objectScan(['person[*].jobs'],{reverse:false,rtn:'value'})(数据));
//=>[[{公司:'},{公司:'}]
。作为控制台包装{最大高度:100%!重要;顶部:0}

结果。人员[0]。作业
请参阅:谢谢,但这是不可扩展的。如果列表中有多个“人”怎么办?