0

i have following data which iam getting from an API call

{
"articles": {
    "articles": [
        {
            "active": true,
            "scanCodes": [
                "356448",
                "875736475896"
            ],
            "stock": {
                "1": {
                    "stockCurrent": 6.0,
                    "stockCustomValue": 0.0,
                    "stockMinimum": 4.0,
                    "stockTarget": 14.0
                }
            },
            "taxRate": 7.0
        },
        {
            "active": true,
            "scanCodes": [
                "682360596",
                "2355235",
                "235235235"
            ],
            "stock": {
                "1": {
                    "stockCurrent": 1.0,
                    "stockCustomValue": 1.0,
                    "stockMinimum": 2.0,
                    "stockTarget": 6.0
                },
                "3": {
                    "stockCurrent": 4.0,
                    "stockCustomValue": 0.0,
                    "stockMinimum": 2.0,
                    "stockTarget": 7.0
                }
            },
            "taxRate": 7.0
        },
        {
            "active": true,
            "scanCodes": [
                "50001112547160"
            ],
            "stock": {
                "1": {
                    "stockCurrent": 1.0,
                    "stockCustomValue": 1.0,
                    "stockMinimum": 2.0,
                    "stockTarget": 7.0
                },
                "2": {
                    "stockCurrent": 2.0,
                    "stockCustomValue": 0.0,
                    "stockMinimum": 0.0,
                    "stockTarget": 1.0
                }
            },
            "taxRate": 7.0
        },
    ]
}
}

I only need stock unique object keys of each article something like this ["1","2","3","5"....etc]

I have tried this code and so many others but still have not gotten the correct result please help me out I will be really thankful to you

resp.data.articles.articles.
                        reduce((p, c) => {
                            let { stock } = c
                            return { ...p.stock + Object.keys(stock) };
                        }, {})

2 Answers 2

1

Here is a simple solution:

const arr = [];
data.articles.articles.forEach(article => arr.push(...Object.keys(article.stock)));
const requiredList = [...new Set(arr)];
Sign up to request clarification or add additional context in comments.

Comments

1

The point here to loop through articles which is an array, and each item has a stock array of objects, and you want the keys.

let art = data.articles.articles;
let stockIds = [];
// loop through the articles
for (let a in art) {
    // loop throgh the stock array in each article
    for (let s in art[a].stock) {
        // Grab the stock key
        stockIds.push(s);
  }
}

//remove duplicates
let uniqueStockIds = uniq = [...new Set(stockIds)]

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.