var phrase1 = [];
phrase1.push({word: "I", x:50, y:50});
phrase1.push({word: "like", x:70, y:50});
phrase1.push({word: "you", x:120, y:50});
var phrase2 = [];
phrase2.push({word: "Well", x:50, y:50});
phrase2.push({word: "I", x:110, y:50});
phrase2.push({word: "tried", x:130, y:50});
phrase2.push({word: "to", x:190, y:50});
var phrase3 = [];
phrase3.push({word: "Hey", x:40, y:50});
phrase3.push({word: "where", x:100, y:50});
phrase3.push({word: "you", x:180, y:50});
phrase3.push({word: "at", x:220, y:50});
var phrases = {"like" : phrase1,
"tried" : phrase2,
"where" : phrase3
};
这是我收到的代码片段。我无法弄清楚如何从 phrases
数组访问单词。假设我想通过 phrase
从 phrase1
访问单词“like”,正确的语法是什么?
尝试了太多的组合,我现在都被难住了。如果有人能指出我正确的方向,那就太好了。谢谢!
我可以访问单个短语
phrase1[1].word
但无法找到通过短语
获取它的方法。
最佳答案
phrases 不是一个数组,它是一个对象。
您可以使用点表示法访问属性,如下所示:
phrases.like[0].word
也可以使用括号表示法,如下:
phrases["like"][0].word
关于Javascript - 如何访问这个字符串数组中的单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46508611/