0

I have a web app which passes delimited fields to another web page. It works fine! But... I want to list the fields (Name) that don't exist in the javascript object. How can this be accomplished?

JS object:

var members = [ { "Class": "E", "Rating": "1000", "ID": "16720664", "Name": "Adeyemon, Murie", "Expires": "1000.10.10" }, 
  { "Class": "B", "Rating": "1735", "ID": "12537964", "Name": "Ahmed, Jamshed", "Expires": "2018.10.18" }, 
  { "Class": "C", "Rating": "1535", "ID": "12210580", "Name": "Attaya, James", "Expires": "2019.01.12" }, 
  { "Class": "F", "Rating": "0001", "ID": "16281977", "Name": "Auld, Thomas", "Expires": "1000.10.10" }, 
  { "Class": "B", "Rating": "1793", "ID": "10117780", "Name": "Badamo, Anthony", "Expires": "2018.09.12" }
] 

JS CODE:

let dataString = "Adeyemon, Murie|Ahmed, Jamshed|Attaya, James|Badamo, Anthony|Birmingham, Gerald|"; 
let splitString = dataString.split("|"); 

for (let i = 0; i < splitString.length; i++) { 
    $temp = splitString[i - 1]; 
    if ($temp > "") { 
        members.find(x => x.Name === $temp); 
    } 
}
7
  • 1
    Could you please show an example of the result? Commented May 16, 2018 at 1:20
  • Why $temp = splitString[i - 1]; ? Commented May 16, 2018 at 1:24
  • If I could do that, I would. But at verlager.com/super-dev.php I am populating a grid that looks up class and rating. But some of the names don't exist in the members object. I want to list them so the user can cut and paste them accurately into a form. Then the grid can be redrawn. Commented May 16, 2018 at 1:25
  • 1
    besides the index error (i -1?) this code members.find(x => x.Name === $temp); ... you are finding it, but throwing away the result of .find Commented May 16, 2018 at 1:27
  • 1
    but when i == 0, $temp = splitString[-1] just use i .. and check $temp.length Commented May 16, 2018 at 1:28

3 Answers 3

1

use filter

var dataString =
  'Adeyemon, Murie|Ahmed, Jamshed|Attaya, James|Badamo, Anthony|Birmingham, Gerald|'
var members = [{"Class":"E","Rating":"1000","ID":"16720664","Name":"Adeyemon, Murie","Expires":"1000.10.10"},{"Class":"B","Rating":"1735","ID":"12537964","Name":"Ahmed, Jamshed","Expires":"2018.10.18"},{"Class":"C","Rating":"1535","ID":"12210580","Name":"Attaya, James","Expires":"2019.01.12"},{"Class":"F","Rating":"0001","ID":"16281977","Name":"Auld, Thomas","Expires":"1000.10.10"},{"Class":"B","Rating":"1793","ID":"10117780","Name":"Badamo, Anthony","Expires":"2018.09.12"}]

var res = dataString.split('|').filter(
  name => !members.map(o => o.Name).find(n => n === name)
).filter(name=>name.trim()!=='')
console.log(res);

Sign up to request clarification or add additional context in comments.

3 Comments

Except that the output formatting is ... a little confusing. Please check localhost/www/verlager/super-dev.php the output could be Fischer, Robert; Petrosian, Tigran Any chance you could improve this?
@verlager,I can't open the url you gave ,please use codepen or jsfiddler instead
I got it to format to my liking. Thanks for your work.
0

You can first create Name to object mapping and then search name from string in this object/map which will cost O(n) for n names.

var members = [ { "Class": "E", "Rating": "1000", "ID": "16720664", "Name": "Adeyemon, Murie", "Expires": "1000.10.10" }, 
  { "Class": "B", "Rating": "1735", "ID": "12537964", "Name": "Ahmed, Jamshed", "Expires": "2018.10.18" }, 
  { "Class": "C", "Rating": "1535", "ID": "12210580", "Name": "Attaya, James", "Expires": "2019.01.12" }, 
  { "Class": "F", "Rating": "0001", "ID": "16281977", "Name": "Auld, Thomas", "Expires": "1000.10.10" }, 
  { "Class": "B", "Rating": "1793", "ID": "10117780", "Name": "Badamo, Anthony", "Expires": "2018.09.12" }
];

var nameMap = members.reduce((prev, next) => {
  prev[next.Name] = next;
  return prev;
}, {});

let dataString = "Adeyemon, Murie|Ahmed, Jamshed|Attaya, James|Badamo, Anthony|Birmingham, Gerald|"; 
let names = dataString.split("|");

let result = names.filter(name => name && !(name in nameMap));

console.log(result);

Comments

0

Try reducing the members array to a Set of names. Then you can filter your splitString array using Set.prototype.has()

const members = [{"Class":"E","Rating":"1000","ID":"16720664","Name":"Adeyemon, Murie","Expires":"1000.10.10"},{"Class":"B","Rating":"1735","ID":"12537964","Name":"Ahmed, Jamshed","Expires":"2018.10.18"},{"Class":"C","Rating":"1535","ID":"12210580","Name":"Attaya, James","Expires":"2019.01.12"},{"Class":"F","Rating":"0001","ID":"16281977","Name":"Auld, Thomas","Expires":"1000.10.10"},{"Class":"B","Rating":"1793","ID":"10117780","Name":"Badamo, Anthony","Expires":"2018.09.12"}]
const dataString = "Adeyemon, Murie|Ahmed, Jamshed|Attaya, James|Badamo, Anthony|Birmingham, Gerald|";

const names = members.reduce((c, {Name}) => c.add(Name), new Set())
const missing = dataString.split('|')
    .filter(name => name.trim() && !names.has(name))
    .join('; ') // output from your comment on another answer

console.info(missing)

I've added in the name.trim() to filter out the empty record created by the trailing | in your dataString.


The reason for creating a Set is to avoid searching the entire members array for each name in dataString. Set.prototype.has() should be O(1)

Comments

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.