0

I would like to know how to create a new object from existing object using javascript.

I have object obj and array arr, from that need to create a object in the expected output format.

var result =getObj("en", obj);

function getObj(lang, obj){
  var newobj = {};
  newobj['url'] = "/"+ lang +"/"+obj[lang].map(e=>e.faq);
  var s ={lang: lang, url: newobj.url};
  newobj['links']=[s];
  return newobj;
}


    var obj={
      "en": [{
      "faq": "faq",
      "about": "about"
      }],
      "hi": [{
      "faq": "aksar-poochhe-jaane-vaale",
      "about": "hamaare-baare"
      }]    
    }

Expected Output:

getObj("en", obj);
//expected result
{
  "url": "/en/faq",
  "links": [
     { lang: 'en', url: '/en/faq' },
     { lang: 'hi', url: '/hi/aksar-poochhe-jaane-vaale' }
   ]
 },{
  "url": "/en/about",
  "links": [
     { lang: 'en', url: '/en/about' },
     { lang: 'hi', url: '/hi/hamaare-baare' }
   ]
 }

getObj("hi", obj);
//expected result
 {
  "url": "/hi/aksar-poochhe-jaane-vaale",
  "links": [
     { lang: 'en', url: '/en/faq' },
     { lang: 'hi', url: '/hi/aksar-poochhe-jaane-vaale' }
   ]
 },{
  "url": "/hi/hamaare-baare",
  "links": [
     { lang: 'en', url: '/en/about' },
     { lang: 'hi', url: '/hi/hamaare-baare' }
   ]
 }
3
  • checkout Object.assign, developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 15, 2019 at 9:53
  • 1
    Your expected output isn't exactly valid. Do you mean an array of two objects? And you'll need at least two loops (or equivalent iteration methods) to get that kind of nested output. Commented Aug 15, 2019 at 10:05
  • The values of obj.en and obj.hi are arrays with a single object in them. Can there be multiple objects there, and what should the result look like then? Commented Aug 15, 2019 at 10:53

1 Answer 1

1

You need to use nested loops. The main loop returns the top-level array of objects, and the inner loop creates the links array within each object.

function getObj(lang, obj) {
  const langObj = obj[lang][0];
  return Object.keys(langObj).map(key => ({
    url: `/${lang}/${langObj[key]}`,
    links: Object.keys(obj).map(l => ({
      lang: l,
      url: `/${l}/${obj[l][0][key]}`
    }))
  }));
}

var obj = {
  "en": [{
    "faq": "faq",
    "about": "about"
  }],
  "hi": [{
    "faq": "aksar-poochhe-jaane-vaale",
    "about": "hamaare-baare"
  }]
};

console.log(getObj("en", obj));
console.log(getObj("hi", obj));

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

3 Comments

thanks for reply, but the url should be ` "/hi/aksar-poochhe-jaane-vaale", "/hi/hamaare-baare" as in expected output`
can you pleas help
Fixed it, I was using the wrong variable in the first url property.

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.