我正在尝试获取另一个数组的下一个值。
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from matplotlib.collections import PatchCollection
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(111)
patches_list = []
color_list = []
patches_list.append(matplotlib.patches.Rectangle((-200,-100), 400, 200))
color_list.append('yellow')
patches_list.append(matplotlib.patches.Rectangle((0,150), 300, 20))
color_list.append('red')
patches_list.append(matplotlib.patches.Rectangle((-300,-50), 40, 200))
color_list.append('#0099FF')
patches_list.append(matplotlib.patches.Circle((-200,-250), radius=90))
color_list.append('#EB70AA')
our_cmap = ListedColormap(color_list)
patches_collection = PatchCollection(patches_list, cmap=our_cmap)
patches_collection.set_array(np.arange(len(patches_list)))
ax.add_collection(patches_collection)
plt.xlim([-400, 400])
plt.ylim([-400, 400])
plt.show()
因此,当我搜索var books = ["book1", "book3"];
var bookprice = [["book1", "$5"], ["book2", "$2"], ["book3", "$7"]];
var list = [];
for (key in bookprice) {
if(bookprice.hasOwnProperty(key)){
list.push(bookprice[key][0]);
}
}
list = ["book1", "book2", "book3"];
时,它将返回book1
。
我坚持下一步需要做的事情。我需要$5
来获取每个索引的索引吗?
答案 0 :(得分:3)
现在,当您获得列表时,可以使用Array.find()
查找具有该书名的内部数组,然后使用[1]
上的find()
获取第二个元素,即价格结果。
使用Array.find()
var bookprice = [
["book1", "$5"],
["book2", "$2"],
["book3", "$7"]
];
var list = ["book1", "book2", "book3"];
var bookName = list[0];
var price = bookprice.find(innerArray => innerArray[0] === bookName)[1];
console.log(price);
FOR IE(纯JS)
var bookprice = [
["book1", "$5"],
["book2", "$2"],
["book3", "$7"]
];
var list = ["book1", "book2", "book3"];
var bookName = list[0];
var price;
for(var i=0; i<bookprice.length; i++){
if(bookprice[i][0] === bookName){
price = bookprice[i][1];
break;
}
}
console.log(price);
答案 1 :(得分:1)
使用Array.find
和destructuration
const bookprice = [
['book1', '$5'],
['book2', '$2'],
['book3', '$7'],
];
const list = ['book1', 'book2', 'book3'];
const bookName = list[0];
const [
,
price,
] = bookprice.find(([
name,
price,
]) => name === bookName);
console.log(price);
正如有人已经在评论中告诉您的那样,最好是创建一个对象而不是一个数组数组。
const bookprice = {
book1: '$5',
book2: '$2',
book3: '$7',
};
const list = ['book1', 'book2', 'book3'];
const bookName = list[0];
const price = bookprice[bookName];
console.log(price);
兼容的IE 11
var bookprice = [
['book1', '$5'],
['book2', '$2'],
['book3', '$7'],
];
var list = ['book1', 'book2', 'book3'];
var bookName = list[0];
var price;
for (var i = 0; i < bookprice.length && price === void 0; i += 1) {
if (bookprice[i][0] === bookName) {
price = bookprice[i][1];
}
}
console.log(price);
答案 2 :(得分:1)
var bookprice = [["book1", "$5"], ["book2", "$2"], ["book3", "$7"]];
var list = ["book1", "book2", "book3"];
var bookName = list[0];
var price = bookprice[list.indexOf(bookName)][1];
console.log(price);
答案 3 :(得分:1)
如果bookprice
是对象,那么检索特定书籍的价格将很容易。 bookprice
应该是:
var bookprice = {
"book1": "$5",
"book2": "$2",
"book3": "$7"
};
然后取回一本书的价格将非常简单:
var book = "book1";
var price = bookprice[book];
bookprice
转换为对象: 使用for
循环(对于旧的浏览器):
var bookpriceObject = {};
for(var i = 0; i < bookprice.length; i++) {
bookpriceObject[bookprice[i][0]] = bookprice[i][1];
}
使用reduce
:
var bookpriceObject = bookprice.reduce(function(acc, book) {
acc[book[0]] = book[1];
return acc;
}, {});
可以使用箭头功能将其缩短:
var bookpriceObject = bookprice.reduce((acc, book) => (acc[book[0]] = book[1], acc), {});
答案 4 :(得分:1)
将Array.find用作https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
var bookprice = [
["book1", "$5"],
["book2", "$2"],
["book3", "$7"]
];
function getBookPrice(bookname) {
let book = bookprice.find(item => item[0] === bookname)
if (book) {
return book[1];
} else {
console.log('No book Found');
return 0;
}
}
console.log(getBookPrice('book1'));
console.log(getBookPrice('book2'));
console.log(getBookPrice('book3'));
console.log(getBookPrice('randombook'));