हमें एक फ़ंक्शन लिखना है जो एक एरे और कितने भी स्ट्रिंग्स को आर्गुमेंट के रूप में लेता है। कार्य यह जांचना है कि क्या स्ट्रिंग्स एरे के भीतर होती हैं। यदि ऐसा होता है, तो हमें उस विशेष तत्व को एरे के सामने ले जाना होगा।
इसलिए, आइए इस फ़ंक्शन के लिए कोड लिखते हैं -
उदाहरण
const arr = ['The', 'weather', 'today', 'is', 'a', 'bit', 'windy.']; const pushFront = (arr, ...strings) => { strings.forEach(el => { const index = arr.indexOf(el); if(index !== -1){ arr.unshift(arr.splice(index, 1)[0]); }; }); }; pushFront(arr, 'today', 'air', 'bit', 'windy.', 'rain'); console.log(arr);आउटपुट
कंसोल में आउटपुट होगा -
[ 'windy.', 'bit', 'today', 'The', 'weather', 'is', 'a' ]