如果没有使用.map()的密钥,如何从数组返回一个对象

时间:2018-01-28 02:33:05

标签: javascript

我正在使用Dialogflow,我想在messages数组中返回没有分配平台密钥的对象的语音值:

"messages": [
    {
      "type": 0,
      "platform": "skype",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "line",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "facebook",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "telegram",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "kik",
      "speech": "FOO"
    },
    {
      "type": 0,
      "speech": "FOO"
    }
  ]

目前,我通过这个丑陋的过程返回值:

messages = messages[messages.length - 1].speech;

我担心的是我不想依赖于返回平台中性消息的数组作为最后一个元素。

目前,这就是我的尝试:

 console.log(messages.map(function(obj) {
    if (!(obj.hasOwnProperty('platform'))){
      return obj;
    }
  }));

但是我收到一条错误,指出TypeError: messages.map is not a function

如何为这种情况设计地图功能

3 个答案:

答案 0 :(得分:2)

基于上面的代码,我想你有类似的东西。我认为你在Map / Associative Array(或JS对象)上运行map,并且map不会在该对象上运行。以下是我认为适合您的修订样本。



let messageMap = {"messages": [
    {
      "type": 0,
      "platform": "skype",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "line",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "facebook",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "telegram",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "kik",
      "speech": "FOO"
    },
    {
      "type": 0,
      "speech": "FOO"
    }
  ]};
  
  // Filter out the messages that don't have a platform defined.
  let platformlessMessages = messageMap['messages'].filter((message) => {
  
    // Return a list of all messages where platform doesn't exists or isn't defined.
    return message.platform === undefined;
  });
  
  console.log(platformlessMessages); // Do whatever you want with that list of objects.




答案 1 :(得分:2)

解决方案1:

您可以循环浏览消息并查找您正在寻找的内容。这是一个有效的解决方案:



var messages =  [
    {
      "type": 0,
      "platform": "skype",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "line",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "facebook",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "telegram",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "kik",
      "speech": "FOO"
    },
    {
      "type": 0,
      "speech": "FOO"
    }
  ]
  for(var obj in messages){
    if(!(messages[obj].hasOwnProperty('platform'))){
        alert(messages[obj].speech);
    }
} 




解决方案2:

您可以同时使用filtermap功能。这是一个有效的解决方案。希望它有所帮助!



var messages =  [
    {
      "type": 0,
      "platform": "skype",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "line",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "facebook",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "telegram",
      "speech": "FOO"
    },
    {
      "type": 0,
      "platform": "kik",
      "speech": "FOO"
    },
    {
      "type": 0,
      "speech": "FOO"
    }
  ]
  var result = messages.filter(function(obj){
    if (!(obj.hasOwnProperty('platform'))){
      return obj;
    }
}).map(m => m.speech);

alert(result); 




答案 2 :(得分:1)

假设messages实际上被定义为数组,我建议使用find函数而不是map。例如:

console.log(messages.find(function (obj) {
  return !obj.hasOwnProperty('platform');
}));

另见:

How to find first element of array matching a boolean condition in JavaScript?