最简洁的方法是在数组对象中查找字符串值?

时间:2017-01-10 16:25:01

标签: javascript arrays loops ecmascript-6 constants

所以我在一个对象(来自API)中有一堆状态代码,它们返回如下:

{
  "location": [
    "HOME_ADDRESS_INCOMPLETE",
    "HOME_MISSING_ADDRESS"
  ],
  "basics": [
    "HOME_MISSING_TYPE"
  ],
  "description": [
    "HOME_MISSING_DESCRIPTION"
  ],
  "immersions": [
    "AT_LEAST_ONE_STAY_REQUIRED",
    "SIMPLE_STAY_MISSING_HOURS",
    "SIMPLE_STAY_MISSING_OFFERED_LANGUAGES",
    "TANDEM_STAY_MISSING_HOURS",
    "TANDEM_STAY_MISSING_OFFERED_LANGUAGES",
    "TANDEM_STAY_MISSING_INTERESTED_LANGUAGES",
    "TEACHER_STAY_MISSING_HOURLY_PRICE",
    "TEACHER_STAY_MISSING_OFFERED_LANGUAGES",
    "TEACHER_STAY_MISSING_WEEKLY_PACKAGES"
  ],
  "rooms": [
    "NO_ROOMS"
  ],
  "photos": [
    "AT_LEAST_ONE_HOME_IMAGE_REQUIRED"
  ],
  "pricing": [
    "MISSING_CURRENCY",
    "SERVICE_WITHOUT_PRICE",
    "DISCOUNT_WITHOUT_PERCENT",
    "ROOM_WITHOUT_PRICE"
  ]
}

密钥名称(如location)与设置向导中的步骤相关联,用户必须根据缺少的内容放置该步骤,该常量由HOME_ADDRESS_INCOMPLETE等常量表示。

从这个对象和一个常量开始,最简洁或最明确的方法是什么,如MISSING_CURRENCY,并返回该常量数组所属的键的名称?

这是我到目前为止所拥有的,但它只是返回数组本身:

const activeStep = Object.values(HomeStatusCodes).filter(statusArray => {
  return statusArray.includes(homeActivationResponse.code)
})

3 个答案:

答案 0 :(得分:2)

Array#find(在该结构的键数组上,来自Object.keys)加Array#indexOf应该这样做:

function find(value) {
  return Object.keys(data).find(function(key) {
    return data[key].indexOf(value) != -1;
  });
}

请注意Array#find是ES2015中的新功能,但可以很容易地进行填充/填充。

示例:



var data = {
  "location": [
    "HOME_ADDRESS_INCOMPLETE",
    "HOME_MISSING_ADDRESS"
  ],
  "basics": [
    "HOME_MISSING_TYPE"
  ],
  "description": [
    "HOME_MISSING_DESCRIPTION"
  ],
  "immersions": [
    "AT_LEAST_ONE_STAY_REQUIRED",
    "SIMPLE_STAY_MISSING_HOURS",
    "SIMPLE_STAY_MISSING_OFFERED_LANGUAGES",
    "TANDEM_STAY_MISSING_HOURS",
    "TANDEM_STAY_MISSING_OFFERED_LANGUAGES",
    "TANDEM_STAY_MISSING_INTERESTED_LANGUAGES",
    "TEACHER_STAY_MISSING_HOURLY_PRICE",
    "TEACHER_STAY_MISSING_OFFERED_LANGUAGES",
    "TEACHER_STAY_MISSING_WEEKLY_PACKAGES"
  ],
  "rooms": [
    "NO_ROOMS"
  ],
  "photos": [
    "AT_LEAST_ONE_HOME_IMAGE_REQUIRED"
  ],
  "pricing": [
    "MISSING_CURRENCY",
    "SERVICE_WITHOUT_PRICE",
    "DISCOUNT_WITHOUT_PERCENT",
    "ROOM_WITHOUT_PRICE"
  ]
};

function find(value) {
  return Object.keys(data).find(function(key) {
    return data[key].indexOf(value) != -1;
  });
}

console.log(find("MISSING_CURRENCY"));




使用ES2015语法时更简洁:

const find = value =>
  Object.keys(data).find(key => data[key].indexOf(value) != -1);

(是的,这真的是一个功能。)这是支持ES2015的浏览器的实时版本:



var data = {
  "location": [
    "HOME_ADDRESS_INCOMPLETE",
    "HOME_MISSING_ADDRESS"
  ],
  "basics": [
    "HOME_MISSING_TYPE"
  ],
  "description": [
    "HOME_MISSING_DESCRIPTION"
  ],
  "immersions": [
    "AT_LEAST_ONE_STAY_REQUIRED",
    "SIMPLE_STAY_MISSING_HOURS",
    "SIMPLE_STAY_MISSING_OFFERED_LANGUAGES",
    "TANDEM_STAY_MISSING_HOURS",
    "TANDEM_STAY_MISSING_OFFERED_LANGUAGES",
    "TANDEM_STAY_MISSING_INTERESTED_LANGUAGES",
    "TEACHER_STAY_MISSING_HOURLY_PRICE",
    "TEACHER_STAY_MISSING_OFFERED_LANGUAGES",
    "TEACHER_STAY_MISSING_WEEKLY_PACKAGES"
  ],
  "rooms": [
    "NO_ROOMS"
  ],
  "photos": [
    "AT_LEAST_ONE_HOME_IMAGE_REQUIRED"
  ],
  "pricing": [
    "MISSING_CURRENCY",
    "SERVICE_WITHOUT_PRICE",
    "DISCOUNT_WITHOUT_PERCENT",
    "ROOM_WITHOUT_PRICE"
  ]
};

const find = value =>
  Object.keys(data).find(key => data[key].indexOf(value) != -1);

console.log(find("MISSING_CURRENCY"));




答案 1 :(得分:0)

您可以迭代密钥并找到属性包含所需项目的密钥。

function getKey(object, item) {
    return Object.keys(object).find(k => object[k].includes(item));
}

var data = { "location": ["HOME_ADDRESS_INCOMPLETE", "HOME_MISSING_ADDRESS"], "basics": ["HOME_MISSING_TYPE"], "description": ["HOME_MISSING_DESCRIPTION"], "immersions": ["AT_LEAST_ONE_STAY_REQUIRED", "SIMPLE_STAY_MISSING_HOURS", "SIMPLE_STAY_MISSING_OFFERED_LANGUAGES", "TANDEM_STAY_MISSING_HOURS", "TANDEM_STAY_MISSING_OFFERED_LANGUAGES", "TANDEM_STAY_MISSING_INTERESTED_LANGUAGES", "TEACHER_STAY_MISSING_HOURLY_PRICE", "TEACHER_STAY_MISSING_OFFERED_LANGUAGES", "TEACHER_STAY_MISSING_WEEKLY_PACKAGES"], "rooms": ["NO_ROOMS"], "photos": ["AT_LEAST_ONE_HOME_IMAGE_REQUIRED"], "pricing": ["MISSING_CURRENCY", "SERVICE_WITHOUT_PRICE", "DISCOUNT_WITHOUT_PERCENT", "ROOM_WITHOUT_PRICE"] };

console.log(getKey(data, 'MISSING_CURRENCY'));

答案 2 :(得分:0)

我去查找地图:

const stepByStatusCode = new Map()
for (const step in HomeStatusCodes) {
    for (const code of HomeStatusCodes[step])
        stepByStatusCode.set(code, step);

然后你可以用最简洁的方式使用

const activeStep = stepByStatusCode.get(homeActivationResponse.code);