简体   繁体   English

如何使用另一个数组获取对象数组中的值 - Javascript

[英]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对您的解决方案的一些修复

  • After split idString , it would result in array of string value, so you have to cast it to number拆分idString ,它将导致字符串值数组,因此您必须将其转换为数字
  • Instead of use 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.

相关问题 如何从一个对象数组中获取值到另一个对象数组中 - How to get values from one array of objects into another array of objects 如何从javascript中的对象数组中获取值 - How to get values from array of objects in javascript 如何获取JavaScript对象数组中的总值 - How to get total of values in array of javascript objects 如何使用 javascript 按属性过滤另一个对象数组的对象数组 - How to filter array of objects by another array of objects by property using javascript 如何使用JavaScript将对象数组组合到另一个数组 - How to combined array of objects one to another array using javascript 如何使用 Javascript 从对象数组中的数组道具中获取不明确的值 - How to get disinct values from an array prop inside an array of objects using Javascript 从 JavaScript 中的另一个对象数组替换一个对象数组的值 - Replace values of an array of objects from another array of objects in JavaScript 如何使用JavaScript从JavaScript对象(包含对象数组)获取所有值 - How to get all values from a JavaScript object (that contains an array of objects) using javascript 如何根据另一个对象数组的值获取一个对象数组键的值? - how to get the values of one array of objects key based on values of another array of objects? 如何使用值数组从数组中动态获取对象列表 - How to dynamically get a list of objects from an array using an array of values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM