[英]How to get values in an array of objects using another array - Javascript
I have a function that interacts with 2 arrays, 1st array is an array of objects that contain my dropdown options, second array is an array of values.我有一个与 2 个数组交互的函数,第一个数组是一个包含我的下拉选项的对象数组,第二个数组是一个值数组。 I'm trying to filter the 1st array to return what has matched the values in my 2nd array.
我正在尝试过滤第一个数组以返回与第二个数组中的值匹配的内容。 How do I achieve this?
我如何实现这一目标?
1st Array:第一个数组:
const books = [
{
label: "To Kill a Mockingbird",
value: 1
},
{
label: "1984",
value: 2
},
{
label: "The Lord of the Rings",
value: 3
},
{
label: "The Great Gatsby",
value: 4
}
]
Code Snippet below:下面的代码片段:
const idString = "1,2,3";
function getSelectedOption(idString, books) {
const ids = idString.split(",");
const selectedOptions = [];
ids.map(e => {
const selected = books.map(options => {
if (options.value === e){
return {
label: options.label,
value: options.value
}
}
})
selectedOptions.push(selected)
})
return selectedOptions
}
Result:结果:
[
[undefined, undefined, undefined, undefined],
[undefined, undefined, undefined, undefined],
[undefined, undefined, undefined, undefined]
]
Expected Result:预期结果:
[
{
label: "To Kill a Mockingbird",
value: 1
},
{
label: "1984",
value: 2
},
{
label: "The Lord of the Rings",
value: 3
}
]
Assuming that value is unique, you can update your code as following to get them in order.假设该值是唯一的,您可以按如下方式更新您的代码以使它们按顺序排列。
const idString = "1,2,3";
function getSelectedOption(idString, books) {
const ids = idString.split(",");
return ids.map(id => books.find(book => book.value == id)).filter(Boolean)
}
You can also filter the books array if you don't care about the order or in case that the value
is not unique.如果您不关心订单或
value
不是唯一的,您还可以过滤书籍数组。
const idString = "1,2,3";
function getSelectedOption(idString, books) {
const ids = idString.split(",");
return books.filter(book => ids.includes(book.value.toString()))
}
Please note that these are O(n*m) algorithms and it should not be used with large sets of data, however if one of the arrays is relatively small you can use it.请注意,这些是 O(n*m) 算法,不应与大量数据一起使用,但是如果其中一个数组相对较小,则可以使用它。
function getSelectedOption(idString, books) {
const idArray = convertStringToArray(idString)
return books.filter(item => idString.includes(item.value))
}
function convertStringToArray(string) {
return string.split(",")
}
Using an array filter
:使用数组
filter
:
function getSelectedOption(idString, books) { const ids = idString.split(","); return books.filter((item) => ids.includes(item.value.toString())); } const books = [{ label: "To Kill a Mockingbird", value: 1 }, { label: "1984", value: 2 }, { label: "The Lord of the Rings", value: 3 }, { label: "The Great Gatsby", value: 4 } ] const idString = "1,2,3"; getSelectedOption(idString, books); console.log(getSelectedOption(idString, books));
Some fixes to your solution对您的解决方案的一些修复
idString
, it would result in array of string value, so you have to cast it to numberidString
,它将导致字符串值数组,因此您必须将其转换为数字map
to get selected, you should use find
find
而不是使用map
来选择 const books = [ { label: 'To Kill a Mockingbird', value: 1 }, { label: '1984', value: 2 }, { label: 'The Lord of the Rings', value: 3 }, { label: 'The Great Gatsby', value: 4 } ] const idString = '1,2,3' function getSelectedOption(idString, books) { const ids = idString.split(',').map(Number) const selectedOptions = [] ids.forEach(e => { const selected = books .map(options => { if (options.value === e) { return { label: options.label, value: options.value } } }) .filter(options => options !== undefined) selectedOptions.push(selected[0]) }) return selectedOptions } console.log(getSelectedOption(idString, books))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.