0

I have an array of objects, and there is a simple array, I want to turn a simple array into a key value and push its elements into each element of the object

const data = {

output: [
  {
    title: "\"Ради будущего своих детей\": Головкин назвал свой любимый 
     город и рассказал о семье",
    time: "10:22",
    image: "https://netstorage- 
     nur.akamaized.net/images/efcfd2c4b999
  },
  {
    title: "\"Изнасиловали, избили, шантажировали\": астанчанка стала 
     жертвой двоих мужчин",
     time: "10:08",
     image: "https://netstorage-
  },
],
href: [
  "https://www.nur.kz/1797318-radi-budusego-svoih-detej-golovkin-nazval- 
   svoj-lubimyj-gorod-i-rasskazal-o-seme.html",
  "https://www.nur.kz/1797004-iznasilovali-izbili-santazirovali- 
    astancanka-stala-zertvoj-dvoih-muzcin.html",
]}

I expect one array of objects example:

 output: [
{
  title: "",
  time: "",
  image: "",
  href: "",
},
{
  title: "",
  time: "",
  image: "",
  href: "",
 },
]
3
  • 4
    Your expected output should be valid code, not a syntax error. Please be much more specific and post code we can work with instead of a collection of syntax errors Commented Jun 3, 2019 at 11:07
  • and what you've tried so far? Commented Jun 3, 2019 at 11:10
  • I tried two for loops, one nested, and it returns an object, but only the href always showed the first element and did not change, Commented Jun 3, 2019 at 11:13

2 Answers 2

3

use map.

const data = {
    output: [
      {
        title: "\"Ради будущего своих детей\": Головкин назвал свой любимый город и рассказал о семье",
        time: "10:22",
        image: "https://netstorage- nur.akamaized.net/images/efcfd2c4b999"
      },
      {
        title: "\"Изнасиловали, избили, шантажировали\": астанчанка стала жертвой двоих мужчин",
         time: "10:08",
         image: "https://netstorage-"
      },
    ],
    href: [
      "https://www.nur.kz/1797318-radi-budusego-svoih-detej-golovkin-nazval- svoj-lubimyj-gorod-i-rasskazal-o-seme.html",
      "https://www.nur.kz/1797004-iznasilovali-izbili-santazirovali-astancanka-stala-zertvoj-dvoih-muzcin.html",
    ]
};

const {output, href}= data;

const finalOutput = output.map((obj, i) => ({...obj, href: href[i]}));

console.log(finalOutput);

Using forEach loop -

const data = {
    output: [
      {
        title: "\"Ради будущего своих детей\": Головкин назвал свой любимый город и рассказал о семье",
        time: "10:22",
        image: "https://netstorage- nur.akamaized.net/images/efcfd2c4b999"
      },
      {
        title: "\"Изнасиловали, избили, шантажировали\": астанчанка стала жертвой двоих мужчин",
         time: "10:08",
         image: "https://netstorage-"
      },
    ],
    href: [
      "https://www.nur.kz/1797318-radi-budusego-svoih-detej-golovkin-nazval- svoj-lubimyj-gorod-i-rasskazal-o-seme.html",
      "https://www.nur.kz/1797004-iznasilovali-izbili-santazirovali-astancanka-stala-zertvoj-dvoih-muzcin.html",
    ]
};

const {output, href}= data;

const finalOutput = [];

output.forEach((obj, idx) => {
    finalOutput.push({...obj, href: href[idx]});
});

console.log(finalOutput);

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

2 Comments

ty it's works perfectly, btw I tried almost the same, but only by forEach
You can use forEach too, but map is more better for one-to-one mapping. You are adding href property to the existing output array. See edit.
0

Please try this code:

var keys = href;
var values = output;

var resultArray = [];
for(var i=0; i<values.length; i++){
  var obj = {};
  for(var j=0; j<keys.length; j++){
     obj[keys[j]] = values[i][j];
  }
   resultArray.push(obj);
}

LMK. thanks.

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.