简体   繁体   中英

filter with object references lodash

I have an array, I need to chunk that array & manipulate parts of the chunks BUT I'd like to it to update my original array

I'm self taught with no computer science background so when it comes to technical inner workings I'm at a disadvantage.

 var markers = ["one","two","three","four","five","six","seven","eight","nine"] var chunks = _.chunk(markers, 5) var result = _.chain(chunks[0]).last().value(); result = 'newValue' console.log(chunks); console.log(markers); //was hopping to get // ["one","two","three","four","newValue","six","seven","eight","nine"] 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script> 

I'm not quite sure I got your requirement right, but this changes the first and last element of each "chunk" (without really splitting the array).

 var data = [ "one","two","three","four","five", "one","two","three","four","five", "one","two","three" ]; var chunkSize = 5, numberOfChunks = Math.ceil(data.length / chunkSize), indexOfFirstElementInChunk, indexOfLastElementInChunk; for (var i = 0; i < numberOfChunks; i++) { indexOfFirstElementInChunk = (i * chunkSize); data[indexOfFirstElementInChunk] = "first element"; indexOfLastElementInChunk = Math.min(data.length, ((i + 1) * chunkSize)) - 1; if (indexOfFirstElementInChunk < indexOfLastElementInChunk) { data[indexOfLastElementInChunk] = "last element"; } } console.log(data); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM