Home > Net >  How many times a string containes in substring - JavaScript
How many times a string containes in substring - JavaScript

Time:07-10

I have an array:

var textWord = [ 'FORTWO', 'ELECTRIC' ];

and a json object:

var modelsList = {
    'FOR FOUR' : null,
    'FOR FOUR DIESEL' : null,
    'FOR FOUR EV' : null,
    'FORTWO CABRIO ELECTRIC DRIVE' : null,
    'FORTWO Cabrio' : null,
    'FORTWO Cabrio Diesel' : null,
    'FORTWO Coupe' : null,
    'FORTWO Coupe Diesel' : null,
    'ROADSTER' : null,
    'ROADSTER Coupe' : null,
}

I want to find how many times find the word of array into json object. The null of object represent the times it was found. My code is:

var textWord = [ 'FORTWO', 'ELECTRIC' ];
var modelsList = {
    'FOR FOUR' : null,
    'FOR FOUR DIESEL' : null,
    'FOR FOUR EV' : null,
    'FORTWO CABRIO ELECTRIC DRIVE' : null,
    'FORTWO Cabrio' : null,
    'FORTWO Cabrio Diesel' : null,
    'FORTWO Coupe' : null,
    'FORTWO Coupe Diesel' : null,
    'ROADSTER' : null,
    'ROADSTER Coupe' : null,
}
var counter = 0;
 for(let i=0; i < textWord.length; i  ){
    for (let modelsListText in modelsList) {
        if (modelsListText.toUpperCase().includes(textWord[i].toUpperCase())) {
            modelsList[modelsListText] = counter  ;
        }
    }
}
console.log(modelsList);

The result is:

{
  'FOR FOUR': null,
  'FOR FOUR DIESEL': null,
  'FOR FOUR EV': null,
  'FORTWO CABRIO ELECTRIC DRIVE': 5,
  'FORTWO Cabrio': 1,
  'FORTWO Cabrio Diesel': 2,
  'FORTWO Coupe': 3,
  'FORTWO Coupe Diesel': 4,
  ROADSTER: null,
  'ROADSTER Coupe': null
}

But the result I want to get is:

{
  'FOR FOUR': null,
  'FOR FOUR DIESEL': null,
  'FOR FOUR EV': null,
  'FORTWO CABRIO ELECTRIC DRIVE': 2, //Match and FORTWO and ELECTRIC
  'FORTWO Cabrio': 1, //Match ONLY FORTWO
  'FORTWO Cabrio Diesel': 1, //Match ONLY FORTWO
  'FORTWO Coupe': 1, //Match ONLY FORTWO
  'FORTWO Coupe Diesel': 1, //Match ONLY FORTWO
  ROADSTER: null,
  'ROADSTER Coupe': null
}

CodePudding user response:

You can check for current value at that time, If there is a value other than null then you can increment it else put 1 inplace of current value

Either use this

modelsList[modelsListText] = (modelsList[modelsListText] ?? 0)   1;

or

modelsList[modelsListText] = modelsList[modelsListText]
                ? modelsList[modelsListText]   1
                : 1;

var textWord = ['FORTWO', 'ELECTRIC'];
var modelsList = {
    'FOR FOUR': null,
    'FOR FOUR DIESEL': null,
    'FOR FOUR EV': null,
    'FORTWO CABRIO ELECTRIC DRIVE': null,
    'FORTWO Cabrio': null,
    'FORTWO Cabrio Diesel': null,
    'FORTWO Coupe': null,
    'FORTWO Coupe Diesel': null,
    ROADSTER: null,
    'ROADSTER Coupe': null,
};

for (let i = 0; i < textWord.length; i  ) {
    for (let modelsListText in modelsList) {
        if (modelsListText.toUpperCase().includes(textWord[i].toUpperCase())) {
            modelsList[modelsListText] = modelsList[modelsListText]
                ? modelsList[modelsListText]   1
                : 1;
        }
    }
}

console.log(modelsList);

CodePudding user response:

Use a regex to remove the need for nested loops.

join your textWord array with an | (which means "or") so the regex looks like /FORTWO|ELECTRIC/g, and use that to match on each key of the object. If a match is made check the length of the array that match returns and update the value of that key's value with that number.

const textWord=['FORTWO','ELECTRIC'];
const modelsList={'FOR FOUR':null,'FOR FOUR DIESEL':null,'FOR FOUR EV':null,'FORTWO CABRIO ELECTRIC DRIVE':null,'FORTWO Cabrio':null,'FORTWO Cabrio Diesel':null,'FORTWO Coupe':null,'FORTWO Coupe Diesel':null,ROADSTER:null,'ROADSTER Coupe':null};

const re = new RegExp(textWord.join('|'), 'g');

for (const prop in modelsList) {
  const match = prop.match(re);
  if (match) modelsList[prop] = match.length;
}

console.log(modelsList);

CodePudding user response:

I'm assuming you want to perform a case-insensitive search, that's why you are using the toUpperCase. Either way, this should work.

var textWord = ['FORTWO', 'ELECTRIC'];
var modelsList = {
    'FOR FOUR': null,
    'FOR FOUR DIESEL': null,
    'FOR FOUR EV': null,
    'FORTWO CABRIO ELECTRIC DRIVE': null,
    'FORTWO Cabrio': null,
    'FORTWO Cabrio Diesel': null,
    'FORTWO Coupe': null,
    'FORTWO Coupe Diesel': null,
    'ROADSTER': null,
    'ROADSTER Coupe': null,
}

for (let modelsListText in modelsList) {
    var counter = 0
    for (let i = 0; i < textWord.length; i  ) {
        if (modelsListText.toUpperCase().includes(textWord[i].toUpperCase())) {
            counter  
            modelsList[modelsListText] = counter
        }
    }
}

console.log(modelsList);

The major difference is where the counter is first declared. You want to reset the counter for each new modelsList item you want to search through.

  • Related