0

let object=
    [
     {
      id:`01`,
      name:`fish`,
      type:`marine`,
     },
     {
      id:`02`,
      name:`fish`,
      type:`fresh`,
     },
     {
      id:`03`,
      name:`fish`,
      type:`tank`,
     },
     {
      id:`04`,
      name:`animal`,
      type:`pet`,
     },
     {
      id:`05`,
      name:`animal`,
      type:`wild`,
     },
     {
      id:`06`,
      name:`animal`,
      type:`zoo`,
     },
     {
      id:`07`,
      name:`food`,
      type:`veg`,
     },
     {
      id:`08`,
      name:`food`,
      type:`non-veg`,
     }
    ]
    
 let test=   object.map((value)=>{
            return [value.name,value.id];
})

console.log(test[0]);

I want to fetch all the name and type so I was trying to fetch by giving test[0] and i was expecting that by this i will get all the name which are present inside object but all I am getting name:"fish", id:"01" I was expecting to fetch all name if I type console.log(test[1]; but it didn't gone with the plan

  • as i am trying to fetch all name and types by like this console.log(test[1], test[2]);
  • expected output like this
fish marine
fish fresh
fish tank
animal pet
animal wild
animal zoo
food veg
food non-veg
  • as i want to send all name and type value in my getValue() function as an argument like this so my test[1] contain all the name value and test[2] contain all the type value getValue(test[1],test[2]);
5
  • 2
    Question is very unclear. No idea what you're trying to achieve, please clarify. Commented Jul 13, 2021 at 5:39
  • console.log(test) will show all of them. Commented Jul 13, 2021 at 5:41
  • Use filter() if you want to get just the elements that have name='fish' and 'type='marine'. Or use find() if you want to return just the first element like that. Commented Jul 13, 2021 at 5:42
  • Can you show how getValue() looks like? Commented Jul 13, 2021 at 5:48
  • I am passinng name and type in getValue() function @PsyGik like this getValue(test[1],test[2]) Commented Jul 13, 2021 at 5:52

3 Answers 3

2

For what it's worth, here is my version:

const arr=
    [ {id:`01`,name:`fish`,  type:`marine`},
      {id:`02`,name:`fish`,  type:`fresh`},
      {id:`03`,name:`fish`,  type:`tank`},
      {id:`04`,name:`animal`,type:`pet`},
      {id:`05`,name:`animal`,type:`wild`},
      {id:`06`,name:`animal`,type:`zoo`},
      {id:`07`,name:`food`,  type:`veg`},
      {id:`08`,name:`food`,  type:`non-veg`} ];
 
 const res=arr.map(({name,type})=>[name,type]);
 
 // a simple getValue function:
 function getValue(a,b){ console.log(a,b); }
 
 // test:
 res.forEach(e=>getValue(...e));
 
 console.log("or, even simpler:");
 arr.forEach(e=>getValue(e.name,e.type));

Sign up to request clarification or add additional context in comments.

3 Comments

I want to use name and type as a argument for getValue() function
Have a look again. Maybe this is more wht you wanted?
Ah I like this better than my version. Spread when passing the args. Nice! +1
0

I am not sure if I understood the use case of getValue, but considering you need to access the name and type, you can use Destructuring assignment in your function.

let object = [{
    id: `01`,
    name: `fish`,
    type: `marine`,
  },
  {
    id: `02`,
    name: `fish`,
    type: `fresh`,
  },
  {
    id: `03`,
    name: `fish`,
    type: `tank`,
  },
  {
    id: `04`,
    name: `animal`,
    type: `pet`,
  },
  {
    id: `05`,
    name: `animal`,
    type: `wild`,
  },
  {
    id: `06`,
    name: `animal`,
    type: `zoo`,
  },
  {
    id: `07`,
    name: `food`,
    type: `veg`,
  },
  {
    id: `08`,
    name: `food`,
    type: `non-veg`,
  },
];

let test = object.map((value) => ([value.name, value.type]));


function getValue(args) {
  const [name, type] = args; // use array destructuring
  console.log(name, type);
}

getValue(test[0])

Comments

0

You could use forEach to pass each name and type property as an argument fo a function.

function getValue(name, type){
   //do something with name and type
}

object.forEach(el => getValue(el.name, el.type));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.