如何从提供键的数组中获取值?

时间:2019-06-05 08:06:06

标签: javascript arrays

我正在尝试从该数组中获取资历值

var jobs= [
  {
    "startDate": "5/2017",
    "endDate": null,
    "isCurrent": true,
    "seniority": "Senior",
  },
  {
    "startDate": "5/2013",
    "endDate": "5/2019",
    "isCurrent": false,
    "seniority": "Junior",
  },
]

并使用此功能获取字段并记录

function ArrayField(array,field){
  for (var index = 0; index < array.length; index++) {
    if (array[index]===field) {
      return index;      
    }
    return false;
  }
}

console.log(ArrayField(jobs,"seniority"))

但这只是注销错误... 我在哪里出问题了?

预期产量

2条独立日志

"Senior"
"Junior"

我尝试过

var jobs= [
  {
    "startDate": "5/2017",
    "endDate": null,
    "isCurrent": true,
    "seniority": "Senior",
  },
  {
    "startDate": "5/2013",
    "endDate": "5/2019",
    "isCurrent": false,
    "seniority": "Junior",
  },
]

function arrayField(array, field) {
  for (var index = 0; index < array.length; index++) {
    if (field in array[index]) {
      return array[index][field]
    }
    return false;
  }
}

console.log(arrayField(jobs,"seniority"))

但这只是注销一个值

"senior"

3 个答案:

答案 0 :(得分:1)

您可以使用map()

获得给定键的值数组。

var jobs = [{ startDate: "5/2017", endDate: null, isCurrent: true, seniority: "Senior" }, { startDate: "5/2013", endDate: "5/2019", isCurrent: false, seniority: "Junior" }];

const getField = (arr, feild) => arr.map(x => x[feild]);
console.log(getField(jobs,"seniority").join())

答案 1 :(得分:0)

无法获得想要的东西。

要么获取函数的输出inode,要么返回一个数组,然后将输出置于函数之外。


内部

您可以检查该属性是否存在并进行输出。

function arrayField(array, field) {
  for (var index = 0; index < array.length; index++) {
    if (field in array[index]) {
      console.log(array[index][field]);
    }
  }
}

var jobs = [{ startDate: "5/2017", endDate: null, isCurrent: true, seniority: "Senior" }, { startDate: "5/2013", endDate: "5/2019", isCurrent: false, seniority: "Junior" }];

arrayField(jobs, "seniority");

外部

function arrayField(array, field) {
  var result = [];
  for (var index = 0; index < array.length; index++) {
    if (field in array[index]) {
      result.push(array[index][field]);
    }
  }
  return result;
}

var jobs = [{ startDate: "5/2017", endDate: null, isCurrent: true, seniority: "Senior" }, { startDate: "5/2013", endDate: "5/2019", isCurrent: false, seniority: "Junior" }];

arrayField(jobs, "seniority").forEach(v => console.log(v));

要获得多个属性,可以添加更多键,并使用rest parameters ...来选择数组中的所有键。

要映射所需的键,可以加入所有所需的属性值。

function getValues(array, ...keys) {
    return array.map(object => keys.map(key => object[key]).join(', '));
}

var jobs = [{ startDate: "5/2017", endDate: null, isCurrent: true, seniority: "Senior" }, { startDate: "5/2013", endDate: "5/2019", isCurrent: false, seniority: "Junior" }];

console.log(getValues(jobs, 'seniority', 'startDate'));

答案 2 :(得分:0)

您可以使用map从给定对象中获取价值,如下所述

var jobs= [
  {
    "startDate": "5/2017",
    "endDate": null,
    "isCurrent": true,
    "seniority": "Senior",
  },
  {
    "startDate": "5/2013",
    "endDate": "5/2019",
    "isCurrent": false,
    "seniority": "Junior",
  },
]

function ArrayField(array,field){
  return array.map((data, i) => data[field])
}
//Iterate all values and console
ArrayField(jobs,"seniority").forEach(value => console.log(value))