简体   繁体   English

Javascript 一个数组的匹配对象

[英]Javascript matching objects of an array

I have this array, I wish to pick out all the names which have a URL which contains '/blekinge'.我有这个数组,我希望挑选出所有包含 URL 的名称,其中包含“/blekinge”。

Any way to do this with map?有什么办法可以用 map 做到这一点? And present in a list?并出现在列表中? I've come this far:我已经走了这么远:

const allaOrter = orter.map((allOrt) =>
<li>{allOrt.url}</li>
)

What I would like to do is to have sort of an ifstatement or a forEach loop that picks out all the URLs which contains /blekinge and present those.我想要做的是有一种 ifstatement 或 forEach 循环来挑选出所有包含 /blekinge 的 URL 并呈现它们。 But I dont know how... The real array I'm working with is much bigger and contains loads of urls.但我不知道如何......我正在使用的真实数组要大得多并且包含大量 url。 Maybe even make new arrays which contains those elements that have a common url. I hope my provided example is enough for someone of you guys to help me out.甚至可以制作新的 arrays,其中包含那些具有公共 url 的元素。我希望我提供的示例足以让你们中的一些人帮助我。 :) :)

orter.json
[{
        "id": "2",
        "namn": "Blekinge",
        "url": "/blekinge"
    },
    {
        "id": "23",
        "namn": "Karlshamn",
        "url": "/blekinge/karlshamn"
    },
    {
        "id": "24",
        "namn": "Karlskrona",
        "url": "/blekinge/karlskrona"
    },
    {
        "id": "25",
        "namn": "Olofström",
        "url": "/blekinge/olofstrom"
    },
    {
        "id": "26",
        "namn": "Ronneby",
        "url": "/blekinge/ronneby"
    }]

Use array.prototype.filter method then map the returned array使用 array.prototype.filter 方法然后 map 返回的数组

const arr = [{
        "id": "2",
        "namn": "Blekinge",
        "url": "/blekinge"
    },
    {
        "id": "23",
        "namn": "Karlshamn",
        "url": "/blekinge/karlshamn"
    },
    {
        "id": "24",
        "namn": "Karlskrona",
        "url": "/blekinge/karlskrona"
    },
    {
        "id": "25",
        "namn": "Olofström",
        "url": "/blekinge/olofstrom"
    },
    {
        "id": "26",
        "namn": "Ronneby",
        "url": "/blekinge/ronneby"
    }]
    
let filtered = (a) => a.url.includes("/blekinge")


console.log(arr.filter(filtered))

then map the result

You should first try to narrow down by matching the object which contains the url /blekinge using the filter method.您应该首先尝试通过使用filter方法匹配包含 url /blekinge的 object 来缩小范围。 Once you have filtered, the resulting array can be used to present a list.过滤后,生成的数组可用于显示列表。

To keep things simple, I implemented an unordered list to present the result, but the core of what you need is in the resulting filtered array.为了简单起见,我实现了一个无序列表来呈现结果,但您需要的核心是结果过滤数组。

 let listElement = document.getElementById('name-list'); let data = [ { "id": "2", "name": "Blekinge", "url": "/blekinge" }, { "id": "23", "name": "Karlshamn", "url": "/blekinge/karlshamn" }, { "id": "24", "name": "Karlskrona", "url": "/blekinge/karlskrona" }, { "id": "25", "name": "Olofström", "url": "/blekinge/olofstrom" }, { "id": "26", "name": "Ronneby", "url": "/test/ronneby" } ]; let updateList = (list, content) => { let li = document.createElement("li"); li.innerHTML = content; list.appendChild(li); }; let filteredData = data.filter(elem => elem.url.indexOf('/blekinge');== -1). filteredData,map(elem => updateList(listElement. elem;name));
 <label for="name-list">Matching names</label> <ul id="name-list"> <ul>

Working Demo:工作演示:

 const data = [{ "id": "2", "namn": "Blekinge", "url": "/blekinge" }, { "id": "23", "namn": "Karlshamn", "url": "/alpha/beta" }, { "id": "24", "namn": "Karlskrona", "url": "/blekinge/karlskrona" }, { "id": "25", "namn": "Olofström", "url": "/abc/def" }, { "id": "26", "namn": "Ronneby", "url": "/blekinge/ronneby" }]; const res = data.filter((obj) => obj.url.indexOf('/blekinge');== -1). console;log(res);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM