javascript数组匹配以显示唯一结果

时间:2016-10-28 08:50:50

标签: javascript arrays

我是javascript数组匹配的新手。

我有两个数组。每个数组都有相同数量的元素:11

txtfilename=['txt1','txt6','txt6','txt6','txt7','txt7','txt8','txt9','txt9','txt9','txt9']
content=['apple from one','six trees','cats','happy dogs','healthy fruit','good job','ask question','check safety','stay in the house','good results','happy holiday']

实际上,txtfilename中元素的相同位置与内容中元素的相同位置匹配。

例如:txtfilename中的第二个元素与内容中的第二个元素匹配。

但是,txtfilename有重复元素,内容元素是唯一的。

我遇到了匹配这两个数组的问题。 我想显示以下两个数组:

txtfilename: txt1
content: apple from one

txtfilename:txt6
content:'six trees','cats','happy dogs'

txtfilename:txt7
content:'healthy fruit','good job'

txtfilename:txt8
content:'ask question'

txtfilename:txt9
content:'check safety','stay in the house','good results','happy holiday'

2 个答案:

答案 0 :(得分:3)

    //Initialising arrays
    String[] txtfilename = new String[] {"txt1", "txt6", "txt6", "txt6", "txt7", "txt7", "txt8",
            "txt9", "txt9", "txt9", "txt9"};

    String[] content = new String[] {"apple from one", "six trees", "cats", "happy dogs",
            "healthy fruit", "good job", "ask question", "check safety", "stay in the house",
            "good results", "happy holiday"};

    //creating a map
    Map<String, String> fileNameWithContents = new HashMap<>();

    //iterating through the array
    for (int i = 0; i < txtfilename.length; i++) {

        //check whether the map contains the txtfilename
        if (fileNameWithContents.containsKey(txtfilename[i])) {

            //appending the new content to the existing content in map
            //Eg: txt6, txt7 and txt9
            StringBuilder val = new StringBuilder(fileNameWithContents.get(txtfilename[i]));
            val.append(", ");
            val.append(content[i]);
            fileNameWithContents.put(txtfilename[i], val.toString());
        } else {
            //adding filename and content to map
            fileNameWithContents.put(txtfilename[i], content[i]);
        }

    }

    // Printing contents of map
    for (Entry<String, String> e : fileNameWithContents.entrySet()) {
        System.out.println("txtfilename: " + e.getKey());
        System.out.println("txtfilename: " + e.getValue());
        System.out.println();
    }

这可以胜任

答案 1 :(得分:0)

在javaScript中,您可以实现MapReduce算法:

var result = txtfilename.map((item, index) => ({"txtfilename": item, "content": content[index]}));
var shift = 0;
txtfilename.forEach((item, index, arr) => {
   if (index < arr.length - 1 && item === arr[index+1])
   {
       result[index + shift].content += ", " + content[index+1];
       result.splice(index + 1 + shift, 1);
       shift --;
   }
});
console.log(result);
// [ { txtfilename: 'txt1', content: 'apple from one' },
//  { txtfilename: 'txt6', content: 'six trees, cats, happy dogs' },
//  { txtfilename: 'txt7', content: 'healthy fruit, good job' },
//  { txtfilename: 'txt8', content: 'ask question' },
//  { txtfilename: 'txt9', content: 'check safety, stay in the house, good  results, happy holiday' } ]

编辑:
首先,我映射textfilename和content以获取对象数组。每个对象都有一个属性txtfilename和content 然后,因为数组按txtfilename排序,我将每个对象与其邻居进行比较。如果两个对象具有相同的键(textfilename属性),我在第一个对象中连接两个字符串(内容属性),并删除已成为冗余的第二个对象。