Using the reduce function to return an array

后端 未结 6 1742
一向
一向 2020-12-13 17:36

Why is it that when I want to use the push function inside the reduce function to return a new array I get an error. However, when I use the concat method inside the reduce

6条回答
  •  旧时难觅i
    2020-12-13 18:20

    reduce can be useful if you need to return an array with multiple items for each item iterated;

    var inputs = media.reduce((passedArray, video) => { passedArray.push("-i"); passedArray.push(video.filepath); return passedArray; }, []);

    Here it's being used to build the input array for ffmpeg;

    [{ name: "bob", filepath: "1.mp4" }, { name: "sue", filepath: "3.mp4" }]

    => ["-i", "1.mp4", "-i", "2.mp4]

提交回复
热议问题