Node.js-用另一个数组对对象数组进行排序

时间:2019-06-11 13:21:53

标签: node.js underscore.js

是否可以对具有另一个数组集的json对象进行排序或重新排列

这是带有url键的JSON对象

var items-array = [
    {
    "_source":{
    "Edition":{
    "Values":{
    "title":"new featured parallex",
    "indexContent":" new "
    }
    },
    "url":"/demo-inline-component-capability/demo-of-featured-parallex"
    },
    "isPromoted":true
    },
    {
    "_source":{
    "Edition":{
    "Values":{
    "title":"demo of event careers",
    "description":"careers",
    "indexContent":" careers demo urrent"
    }
    },
    "url":"/demo-inline-component-capability/demo-of-event-card"
    },
    "isPromoted":true
    },
    {
    "_source":{
    "Edition":{
    "Values":{
    "title":"reference event cards",
    "indexContent":" reference event cards <cmp id=\"jvcbt0if\" class=\"cmp\" contenteditable=\"false\"> orange"
    }
    },
    "url":"/demo-inline-component-capability/demo-of-ref-event-card"
    },
    "isPromoted":true
    },
    {
    "_source":{
    "Edition":{
    "Values":{
    "title":"demo of video playlists",
    "indexContent":" demo "
    }
    },
    "url":"/demo-inline-component-capability/demo-of-video-playlist"
    },
    "isPromoted":true
    },
    {
    "_source":{
    "Edition":{
    "Values":{
    "title":"demo of data point set careers",
    "description":"careers",
    "indexContent":" careers demo of data point set red"
    }
    },
    "url":"/demo-inline-component-capability/demo-of-data-point-set"
    },
    "sort":[
    1555320208440
    ]
    },
    {
    "_source":{
    "Edition":{
    "Values":{
    "title":"Xfinity TV’sa Lunar New Year collection is a celebration of the vast contributions the Asian American community makes to entertainment across comcast careers",
    "description":"mobile name",
    "indexContent":" mobile name "
    }
    },
    "url":"/lunar-festival"
    },
    "sort":[
    1551093922066
    ]
    }
    ]

与网址的排序数组匹配

var sortingArr = ["/demo-inline-component-capability/demo-of-video-playlist","/demo-inline-component-capability/demo-of-featured-parallel","/demo-inline-component-capability/demo-of-ref-event-card","/demo-inline-component-capability/demo-of-event-card"]

很遗憾,我没有任何要跟踪的ID。我需要优先处理items-array,以使其与sortingArr尽可能接近。

任何想法如何做到这一点。

3 个答案:

答案 0 :(得分:0)

下面的代码将输出一个新数组newArr,它按提供的顺序查找所有内容。它不会包含sortingArr中没有的任何项目。

var newArr = [];

sortingArr.forEach(s => {
    var i = itemsArray.find(o => {
        return o["_source"]["url"] === s;
    })

    if(i != null) {
        newArr.push(i);
    }
});

newArr.forEach(o => {
    console.log(o);
});

此外,sortingArr的格式不正确。应该是:

var sortingArr = ["/demo-inline-component-capability/demo-of-video-playlist","/demo-inline-component-capability/demo-of-featured-parallel","/demo-inline-component-capability/demo-of-ref-event-card","/demo-inline-component-capability/demo-of-event-card"]

答案 1 :(得分:0)

嗨,我带来了这个O(n)解决方案 您可以创建一个具有键作为url和值作为与URL对应的对象列表的对象 然后根据sortArray中存在的排序顺序URL,可以遍历sort数组并获取对象中相应的对象列表,然后合并它们以获得最终数组

function sortItem(items_array, sortUrl) {
    let urlMap = {}
    for (item of items_array) {
        let url = item._source.url
        if (!urlMap.hasOwnProperty(url)) {
            urlMap[url] = []
        }
        urlMap[url].push(item)
    }
    let finalList =[]
    for (url of sortUrl){
        if(urlMap.hasOwnProperty(url)){
        let itemList = urlMap[url]
        finalList = finalList.concat(itemList)
        }
    }

    return finalList
}

sortUrl是用于对items_array进行排序的网址列表

var sortingArr = ["/demo-inline-component-capability/demo-of-video-playlist","/demo-inline-component-capability/demo-of-featured-parallel","/demo-inline-component-capability/demo-of-ref-event-card","/demo-inline-component-capability/demo-of-event-card"]

答案 2 :(得分:0)

您可以为此实现自定义sort

typedef vector<int>

示例

int find (vi v,int val)
{
    int first=0;
    while (v.size()!=first) {
        if (v.at(first)==val) return first;
        ++first;
    }
    return -1;
}