增加JS数组中的对象键

时间:2018-10-11 15:41:18

标签: javascript arrays ecmascript-6

let $object= [
        {
            tagName: "01",
            contentID: [100, 200],
            occurrences: 2
        },
        {
            tagName: "02",
            contentID: [200, 300],
            occurrences: 2
        },
        {
            tagName: "03",
            contentID: [100, 200, 300],
            occurrences: 3
        },
        {
            tagName: "04",
            contentID: [300],
            occurrences: 1
        }];

我想在匹配occurrences时增加tagName的值。我应该如何增加occurrences的价值/数量?

// let tagManagerArr = []; //全局范围

for (let content of contents) {
        contentId = content.contentID;
        entityType = content.entityType;
        let tags = content.tags,
            check = false;//check gives me whether a tagName is already present in the '$object' array..
     for (let tagName of tags) {
                console.log("---------------------------------------")
                 tagManagerArr.forEach(
                     (arrayItem) => {
                         if (arrayItem.tagName === tag) {
                            check = true;
                        }
                     }
                 );

                //tagManagerArr.find(
                 //   (element) => {
                  //      if (element.tagName === tagName) {
                 //           check = true;
                  //      }
                  //  });
                if (!check) {
                    tagObject = {};
                    tagObject['tagName'] = tagName;
                    tagObject['contentID'] = [];
                    tagObject['occurrences'] = 1;

                    tagObject['contentID'].push(contentId);
                } else {
                    tagManagerArr.find(
                        (element) => {
                            if (element.tagName === tagName) {
                                if (!element.contentID.includes(contentId)) {
                                    element.contentID.push(contentId);
                                }
                                element.occurrences += 1;
                            }
                        });
                }

                tagManagerArr.push(tagObject);
            }
}

这工作正常,但出现了错误的情况。任何线索吗?

2 个答案:

答案 0 :(得分:1)

使用从标记名到属性的映射将更加容易和高效:

// pick a better name
const map = new Map();

for (const content of contents) {
    for (const tagName of content.tags) {
        let tagObject = map.get(tagName);

        if (tagObject === undefined) {
            map.set(tagName, tagObject = {
                contentID: new Set(),
                occurrences: 0,
            });
        }

        tagObject.occurrences++;
        tagObject.contentID.add(content.contentID);
    }
}

然后您可以将其转换为数组格式:

const tagManagerArr = Array.from(map,
    ([tagName, {contentID, occurrences}]) => ({
        tagName,
        contentID: Array.from(contentID),
        occurrences,
    }));

答案 1 :(得分:1)

改为使用对象。 数据格式取决于您-使其易于使用。

let tags = {
    "01": {
       contentID: [10, 20],
       occurrences: 0
    },
    "02": {
       contentID: [10, 20],
       occurrences: 0
    }
}
// get all tag names
const tagNames = Object.keys(tags);
// increment tag value
tags["01"].occurrences++;

Update: you can sort the array as well.
Object.keys(tags).map(tagName => tags[tagName]).sort((tag1, tag2) => {
    if (tag1.occurrences > tag2.occurrences) {return -1}
    if (tag1.occurrences < tag2.occurrences) {return 1}
    return 0;
});