从ES5等效对象中获取数组

时间:2018-03-13 12:23:00

标签: javascript arrays json object

coursesCompleted [
  {
    "coursename": "ITSM",
    "relatedTests": [
      {
        "testid": "8a754f2cdbf85700bde15901cf9619dc",
        "testname": "Project plan Fundamentals"
      },
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "7ce4fa53dbe0d300bde15901cf96192f"
  },
  {
    "coursename": "CSA",
    "relatedTests": [
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "fea27b96db285300bde15901cf96194b"
  }]

构建此对象我想获得 relatedTests 数组"courseid": "7ce4fa53dbe0d300bde15901cf96192f"

我该怎么做??

5 个答案:

答案 0 :(得分:1)

您必须使用array.filter

参见在线文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter



const coursesCompleted = [
  {
    "coursename": "ITSM",
    "relatedTests": [
      {
        "testid": "8a754f2cdbf85700bde15901cf9619dc",
        "testname": "Project plan Fundamentals"
      },
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "7ce4fa53dbe0d300bde15901cf96192f"
  },
  {
    "coursename": "CSA",
    "relatedTests": [
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "fea27b96db285300bde15901cf96194b"
  }]
  
  console.log(


  coursesCompleted.filter((item) => {
    return item.courseid === '7ce4fa53dbe0d300bde15901cf96192f'
  })[0].relatedTests


  );




答案 1 :(得分:1)

在数组 coursesCompleted 上使用 find() ,找出所请求的courseId的项目。

然后从返回的对象中获取 relatedTests ;



let coursesCompleted =  [
  {
    "coursename": "ITSM",
    "relatedTests": [
      {
        "testid": "8a754f2cdbf85700bde15901cf9619dc",
        "testname": "Project plan Fundamentals"
      },
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "7ce4fa53dbe0d300bde15901cf96192f"
  },
  {
    "coursename": "CSA",
    "relatedTests": [
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "fea27b96db285300bde15901cf96194b"
  }]
  
let output = coursesCompleted.find(a => a.courseid == "7ce4fa53dbe0d300bde15901cf96192f").relatedTests

console.log(output)




答案 2 :(得分:1)

  

我想获得具有" courseid"的relatedTests数组:   " 7ce4fa53dbe0d300bde15901cf96192f"

使用reduce

var courseId = "7ce4fa53dbe0d300bde15901cf96192f";
var output = coursesCompleted.reduce( ( acc, c ) => 
    acc.concat( c.courseid == courseId ? c.relatedTests : [] ) ,[]) ;

<强>演示

&#13;
&#13;
var coursesCompleted = [{
    "coursename": "ITSM",
    "relatedTests": [{
        "testid": "8a754f2cdbf85700bde15901cf9619dc",
        "testname": "Project plan Fundamentals"
      },
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "7ce4fa53dbe0d300bde15901cf96192f"
  },
  {
    "coursename": "CSA",
    "relatedTests": [{
      "testid": "aaf18ceedb285300bde15901cf961934",
      "testname": "Admin"
    }],
    "courseid": "fea27b96db285300bde15901cf96194b"
  }
];

var courseId = "7ce4fa53dbe0d300bde15901cf96192f";
var output = coursesCompleted.reduce((acc, c) =>
  acc.concat(c.courseid == courseId ? c.relatedTests : []), []);
console.log( output );
&#13;
&#13;
&#13;

注意

  • 此答案假设可能有多个行具有相同的courseId

ES5等效

var output = coursesCompleted.reduce( function( acc, c ) { 
    return acc.concat( c.courseid == courseId ? c.relatedTests : [] ) 
},[]) ;

答案 3 :(得分:1)

var coursesCompleted =

[
  {
    "coursename": "ITSM",
    "relatedTests": [
      {
        "testid": "8a754f2cdbf85700bde15901cf9619dc",
        "testname": "Project plan Fundamentals"
      },
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "7ce4fa53dbe0d300bde15901cf96192f"
  },
  {
    "coursename": "CSA",
    "relatedTests": [
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "fea27b96db285300bde15901cf96194b"
  }];
  
var course=  coursesCompleted.filter((course)=> course.courseid=="7ce4fa53dbe0d300bde15901cf96192f");
console.log(course[0].relatedTests)

答案 4 :(得分:1)

let coursesCompleted = [{
    "coursename": "ITSM",
    "relatedTests": [{
        "testid": "8a754f2cdbf85700bde15901cf9619dc",
        "testname": "Project plan Fundamentals"
      },
      {
        "testid": "aaf18ceedb285300bde15901cf961934",
        "testname": "Admin"
      }
    ],
    "courseid": "7ce4fa53dbe0d300bde15901cf96192f"
  }, {
    "coursename": "CSA",
    "relatedTests": [{
      "testid": "aaf18ceedb285300bde15901cf961934",
      "testname": "Admin"
    }],
    "courseid": "fea27b96db285300bde15901cf96194b"
  }
]

let result = coursesCompleted.filter(x => x.courseid==='7ce4fa53dbe0d300bde15901cf96192f')[0].relatedTests

console.log(result)