Home > Net >  javascript obj if condition
javascript obj if condition

Time:03-30

I am having trouble on if condition in object array, what I want is add a total count if the id is equal to authorsid

I have this code

let arrAuthorPublished = { name: [], totalCount: [] }
for(let x = 0; x < counts; x  ){
  if(ids == authorsid){
    arrAuthorPublished.name.push(authorName);
    arrAuthorPublished.totalCount.push(total = total   1);
  } else {
    console.log("fail")
  }
}

the value of ids are

[440944522, 440936792, 440936202, 440930072]

and the value of authors are:

[440944522, 440936792, 440936202, 440930072]

UPDATE

const author = async()=>{
    return new Promise((resolve, reject)=>{
        var getAuthor = (`SELECT id, name FROM Author`, 1000, data=>{
            let obj = { ids: [], authorName: [] }
            for(let x = 0; x < data.length; x  ){
                obj.ids.push(data[x][0]);
                obj.authorName.push(data[x][0]);
            }
            return resolve(obj);
        })
    })
}
const transaction = async()=>{
    return new Promise((resolve, reject)=>{
          var getAuthorTransac = (`SELECT  rAuthors, exp_fAuthor FROM Transaction`, 1000, data=>{
              let arrAuthorPublished = { authors: [], counts: [] }
              for(let x = 0; x < data.length; x  ){
                    arrAuthorPublished.authors.push(data[x][0]);
                    arrAuthorPublished.counts.push(data[x][1]);
              }
              return resolve(arrAuthorPublished);
          })
      })
}

const compute = async() => {
    var {ids, authorName} = await author();
    var {authors, counts} = await transaction();
    let total = 0;
    let arrAuthorPublished = { name: [], totalCount: [] }
    for(let x = 0; x < counts; x  ){
        if(ids == authors){
            arrAuthorPublished.name.push(authorName);
            arrAuthorPublished.totalCount.push(total = total   1);
        }
    }
    return arrAuthorPublished;
}

Please dont read this linePlease dont read this linePlease dont read this linePlease dont read this linePlease dont read this linePlease dont read this linePlease dont read this linePlease dont read this linePlease dont read this linePlease dont read this line

CodePudding user response:

I think you are trying to accomplish something like this:

const ids = [440944522, 440936792, 440936202, 440930072];
const authors = [440944522, 440936792, 440936202, 440930072];

const countMatches = (id, authors) => authors.filter(author=>author===id).length;

const totals = [];

for(let i = 0; i < ids.length; i  ){
  totals.push(countMatches(ids[i], authors))
}

CodePudding user response:

.filter() one array and compare:

arrA.filter(a => arrB.includes(a));

Then get the count by .length

const obj = {
  arrA: [440944522, 440936792, 440936202, 440930072],
  arrB: [440944522, 440936792, 440936202, 440930071]
};

const idTotal = obj.arrA.filter((a, i) => obj.arrB.includes(a));
console.log(idTotal.length);

CodePudding user response:

let arrAuthorPublished = { name: [], totalCount: [] }
for(let x = 0; x < counts; x  ){
    for (let y = 0; y < ids.len; y  ){
        if(ids[y] != authorsid[y]){
            console.log("fail")
        } else {
            arrAuthorPublished.name.push(authorName)
            arrAuthorPublished.totalCount.push(total = total   1);
        }
    }
}
  • Related