-1

From this array, i want to select only flipkart and create a new array which contains only flipkart.

json = [
  {
    "amazon": []
  },
  {
    "flipkart": {
      "product_store": "Flipkart",
      "product_store_logo": "https://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png",
      "product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvcmVhbG1lLTItZGlhbW9uZC1ibGFjay02NC1nYi9wL2l0bWY4NXNjeHlnaHh3Y2g_cGlkPU1PQkY4NVVZQ0RERlZFRE0mYWZmaWQ9YXJ1bmJhYnVs",
      "product_price": "10990",
      "product_mrp": "0",
      "product_offer": "",
      "product_color": "black",
      "product_delivery": "3-4",
      "product_delivery_cost": "0",
      "is_emi": "1",
      "is_cod": "1",
      "return_time": "10 Days"
    }
  },
  { "snapdeal": [] },
  { "ebay": [] },
  { "paytm": [] },
  { "croma": [] },
  { "yebhi": [] },
  { "indiatimes": [] },
  { "homeshop18": [] },
  { "naaptol": [] },
  { "infibeam": [] },
  { "tatacliq": [] },
  { "shopclues": [] },
  { "paytmmall": [] },
  { "gadgets360": [] },
  { "mi": [] },
  { "2gud": [] }
];

edit: above problem is solved, the solution was,

this.store_array.filter(el => {
            if(Array.isArray(el[Object.keys(el)[0]])){
                return el[Object.keys(el)[0]].length;
            }
            else if(typeof el[Object.keys(el)[0]]==="object"){
                this.brands.push(el[Object.keys(el)[0]])
                return el[Object.keys(el)[0]]
            }
        });

and https://stackoverflow.com/users/8802812/kullya was the one who answered it first but i can't find his answer, since i am new to stackoverflow, all credits goes to user. thank you very much.

Akash Jain
  • 316
  • 2
  • 10
  • 3
    Your data is not consistent. All the items in the array holds a one-key object that holds an array, but the `flipkart` holds an object. Also your data is an **array**, not a plain object (dictionary). Therefore you can use [`Array.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to filter out the values – Harun Yilmaz Jul 28 '20 at 06:23
  • hey i dont know how to use stackoverflow, yet but there was a person who answered this question and i think he deleted his answer, so i don't know how to accept his answer. his answer was correct and now problem is solved. – Akash Jain Jul 28 '20 at 08:06
  • Afaik, only the moderators and the poster of the answer are able to view the deleted answer. maybe the poster sees your comment and undelete their answer. And if they won't, maybe you can post the solution as an answer for the future readers. Welcome btw :) – Harun Yilmaz Jul 28 '20 at 08:14

3 Answers3

2

You could have a function as below and pass on for which data you might need to find/filter (both works :) ):

var arr= [ { "amazon": [] }, { "flipkart": { "product_store": "Flipkart", "product_store_logo": "https://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png", "product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvcmVhbG1lLTItZGlhbW9uZC1ibGFjay02NC1nYi9wL2l0bWY4NXNjeHlnaHh3Y2g_cGlkPU1PQkY4NVVZQ0RERlZFRE0mYWZmaWQ9YXJ1bmJhYnVs", "product_price": "10990", "product_mrp": "0", "product_offer": "", "product_color": "black", "product_delivery": "3-4", "product_delivery_cost": "0", "is_emi": "1", "is_cod": "1", "return_time": "10 Days" } }, { "snapdeal": [] }, { "ebay": [] }, { "paytm": [] }, { "croma": [] }, { "yebhi": [] }, { "indiatimes": [] }, { "homeshop18": [] }, { "naaptol": [] }, { "infibeam": [] }, { "tatacliq": [] }, { "shopclues": [] }, { "paytmmall": [] }, { "gadgets360": [] }, { "mi": [] }, { "2gud": [] } ]

function getData (commerceName){
    return arr.filter(x=>x[commerceName])
}
// using `find`
// function getData (commerceName){
//  return arr.find(x=>x[commerceName])
//} 

let res = getData('flipkart')

//let res1 = getData('snapdeal')

console.log(res)
KcH
  • 3,302
  • 3
  • 21
  • 46
1
const result = json.find(a => a.hasOwnProperty('flipkart'));
const flipkartData = result ? [result.flipkart] : [];
0

You can use the .find method of Array and look for the object whose keys contain "flipkart".
(If you actually want just the object instead of wrapping it in an array, you can just omit the [] in the last step.)

const
  allData = getAllData(),
  flipkart = allData.find(currentItem =>
    Object.keys(currentItem).includes("flipkart"));
console.log([flipkart]);

function getAllData(){
  return [
    {
      "amazon": []
    },
    {
      "flipkart": {
        "product_store": "Flipkart",
        "product_store_logo": "https://images-api.datayuge.in/image/ZmxpcGthcnRfc3RvcmUucG5n.png",
        "product_store_url": "https://price-api.datayuge.com/redirect?id=aHR0cHM6Ly9kbC5mbGlwa2FydC5jb20vZGwvcmVhbG1lLTItZGlhbW9uZC1ibGFjay02NC1nYi9wL2l0bWY4NXNjeHlnaHh3Y2g_cGlkPU1PQkY4NVVZQ0RERlZFRE0mYWZmaWQ9YXJ1bmJhYnVs",
        "product_price": "10990",
        "product_mrp": "0",
        "product_offer": "",
        "product_color": "black",
        "product_delivery": "3-4",
        "product_delivery_cost": "0",
        "is_emi": "1",
        "is_cod": "1",
        "return_time": "10 Days"
      }
    },
    { "snapdeal": [] },
    { "ebay": [] },
    { "paytm": [] },
    { "croma": [] },
    { "yebhi": [] },
    { "indiatimes": [] },
    { "homeshop18": [] },
    { "naaptol": [] },
    { "infibeam": [] },
    { "tatacliq": [] },
    { "shopclues": [] },
    { "paytmmall": [] },
    { "gadgets360": [] },
    { "mi": [] },
    { "2gud": [] }
  ];
}
Cat
  • 4,141
  • 2
  • 10
  • 18