1

I have an array and object of items, I want to check each item in that array if its path has that object name, I push it in that object array.

So far this is all good, now if no match found I want to create a new item based on that array item name and push it inside it!

All my attempts ending in duplicated value, I think I need a third object/array I just can't think it anymore

To explain better:

cList = {
  "rList": {
    "Significant": [
      {
        "Path": "Significant\\Significant Charts",
        "Name": "Charts"
      }
    ]
  },
};

and

SSList = {
  value: [
    {
      "Name": "Test long name",
      "Path": "/someFolder/Test long name",
    },
    {
      "Name": "Untitled",
      "Path": "/Significant/Untitled",
    }
  ]
};

My current code

for (var cFolder in this.cList.rList) {
        this.SSList.forEach((ssFile)=> {
          if(ssFile.Path.indexOf(cFolder) >= 0){
            this.cList.rList[cFolder].push(ssFile);
          }
        });
      }

The first item in SSList will not be pushed since it doesn't match, I want to create a array and push it to inside rList

var folderName = ssFile.Path.split("/");
this.cList.rList[folderName[1]].push(ssFile);

1 Answer 1

1

One way to do it is to flip your inner and outer loops

let found = false;
this.SSList.value.forEach((ssFile) => {
    for (var cFolder in this.cList.rList) {
        if(ssFile.Path.indexOf(cFolder) >= 0){
            found = true;
            break;
        }
    }
    if (found) {
        this.cList.rList[cFolder].push(ssFile);
    } else {
        folderName = ssFile.Path.split("/");
        if (!(folderName[1] in this.cList.rList))
            this.cList.rList[folderName[1]] = [];
        this.cList.rList[folderName[1]].push(ssFile);
    }
    found = false;
});
Sign up to request clarification or add additional context in comments.

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.