简体   繁体   English

如何使用另一个数组 javascript 中的值从一个数组中检索

[英]How to retrieve from one array using the value in another array javascript

I have an array named x like below.我有一个名为x的数组,如下所示。

x = [
  { id: 1, content: 'abc' },
  { id: 2, content: 'cde' },
  { id: 3, content: 'xyz' }
]

I have another array y like this.我有另一个像这样的数组y

y = [1, 3]

I want to fetch all data from x by mapping the id of x to the values in y .我想通过将x的 id 映射到y中的值来从x获取所有数据。 So my output will be like below.所以我的 output 将如下所示。

[
  { content: 'abc' },
  { content: 'xyz' }
]

How can I achieve this in Javascript?如何在 Javascript 中实现这一点?

You can use array.filter (+ some to comare with other array) along with array.map您可以使用array.filter (+一些与其他数组比较)以及array.map

 let x=[ {id: 1, content:'abc'}, {id: 2, content:'cde'}, {id: 3, content:'xyz'}]; let y=[1,3]; let result = x.filter(obj => y.some(yy => yy === obj.id)).map(({content}) => ({content})); console.log(result); //or let result2 = x.filter(obj => y.some(yy => yy === obj.id)).map(x => x.content); console.log(result2);

Using Array.filter() and Array.map() you can get the results.使用 Array.filter() 和 Array.map() 您可以获得结果。

Suppose:认为:

const x = [
  {id: 1, content:'abc'},
  {id: 2, content:'cde'},
  {id: 3, content:'xyz'},
];

const y = [1,3];

const result = x.filter(item => y.includes(item.id)).map(item => {
    return {
        content: item.content
    }
});
console.log(result);

Output: Output:

{content:'abc'}
{content:'xyz'}

you can use array reduce for that:您可以为此使用数组减少:

 const x = [ { id: 1, content: 'abc' }, { id: 2, content: 'cde' }, { id: 3, content: 'xyz' } ] const y = [ 1, 3 ] const result = x.reduce((a,{id,content})=> { if (y.some(eY=>eY=== id)) a.push({ content } ) return a },[]) console.log( result )

A for-loop can be used to extract elements with matching IDs. for 循环可用于提取具有匹配 ID 的元素。

Here is a working demo using a for loop:这是一个使用 for 循环的工作演示:

<!DOCTYPE html>
<html>
    <head>
        <title>Demo</title>
    </head>
    <body>
        <button onclick="myFunction()">Find elements</button>
        <p id="result"></p>
        <script>
            var x = [
                {id: 1, content:'abc'},
                {id: 2, content:'cde'},
                {id: 3, content:'xyz'}
            ]

            var y = [1, 3]
            var result = []

            function myFunction() {
                for(let i = 0; i < x.length; i++) {
                    if( y.indexOf(x[i].id) >= 0 ) {
                        // Add content of matching elements to result array
                        result.push(x[i].content)
                    }
                }
                document.getElementById("result").innerHTML = result
            }
        </script>
    </body>
</html>

Output: Output:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从JavaScript多维数组检索值 - How to retrieve value from javascript multidimensional array 如何从JavaScript中的一个数组和一个int文字的json检索值 - How to retrieve value from json of one array and one int literal in javascript 如何将数组对象中的第一个值检索到Javascript中的新数组中? - How to retrieve first value from Array objects into a new Array in Javascript? 如何将数组值从一个数组映射到另一个数组JavaScript? - How to Map Array values from one Array to Another Array JavaScript? 在 Javascript 中的另一个数组中搜索一个数组中的每个值 - Search for every value from one array in another array in Javascript 如何在另一个数组中检索具有值的数组? - How to retrieve an array with value within another array? 如何在 JavaScript 中从另一个数组中过滤一个数组中的对象? - How to filter objects from one array from another array in JavaScript? 如何从数组中显示与另一个数组的值相关的值,javascript - How to show a value from array that relates to the value of another array, javascript JavaScript:如何从数组中检索随机项目(锚定到另一个项目)? - JavaScript: How to retrieve a random item (anchored to another item) from array? JavaScript-使用键数组从嵌套对象中检索值 - JavaScript - retrieve value from nested object, using array of keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM