0

The following code, returns an array of unique value from the data array. However, what I am trying to achieve is return true if there is a uniqueness found at value1:value2. As you can see from the code below value1:value2:value3 and value1:value2:value4 match this criteria. A lodash technique to produce this functionality would be ideal, however for the sake of understanding a solution I would gladly welcome plain 'ol' javascript.

NOTE jQuery and HTML have been included to illustrate my question. The javascript I am after, would be running on NodeJS.

UPDATE

Expected output from running the code below would true as there is a match between value1:value2:value3 and value1:value2:value4 i.e. value1:value2 exist in both values.

UPDATE

Please see the following fiddle for an example of the output I am after.

UPDATE

Included is an example of the output I am after. This is the desired functionality, however I would like a lodash way of achieving the same result.

$(document).ready(function() {
  var data = [
    'value1:value2:value3',
    'value1:value2:value4',
    'value5:value6:value7',
    'value1:value2:value3'
    ];
  var partialValueMatch = function(data) {
    var lookup = _.uniqBy(data, function(str){
      return str.substring(0, str.lastIndexOf(':'))
    });
    if(lookup.length < data.length) return true;
    return false;
  }
  var $output = $('#output');
  $output.html(partialValueMatch(data));
});
#output {
  background: #fafafa;
  border: 1px solid #d8d8d8;
  padding: 5px 10px;
  font-family: monospace;
  border-radius: 2px;
  color: red;
  }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
<div id="output"></div>

3
  • Can you explain a bit better, given a data set what would be the ideal output? Commented May 24, 2016 at 10:21
  • @AvraamMavridis please see UPDATE Commented May 24, 2016 at 10:30
  • @JagsSparrow 's answer will probably work then. Commented May 24, 2016 at 10:32

1 Answer 1

2

You can use _.uniqBy

 var data = [
    'value1:value2:value3',
    'value1:value2:value4',
    'value5:value6:value7',
    'value1:value2:value3'
    ];

var output = _.uniqBy(data,function(str){
   return str.substring(0,str.lastIndexOf(':'))
 });
console.log(output);
console.log('Is duplicates found:',output.length<data.length);

This will give you uniq data as below

//["value1:value2:value3", "value5:value6:value7"]
//Is duplicates found: true
Sign up to request clarification or add additional context in comments.

7 Comments

Awesome and usable for my needs. However, is there any way I could make it return true?
Where need return true in output, Plz share output sample with us.
In your example, the length of the data array has changed, so I guess a comparison could be made on the original input array length and the output length, if it's less than the original.. return true.
Yep, You can get it by comparing input & output data length.
I have included a fiddle here jsfiddle.net/s4zpoaw1 to better explain my example, using the method you described.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.