Skip to content

Define iterator for Napi::Array for using with ranged for-loop or STL algorithms #830

Closed
@aminya

Description

@aminya

Currently, the only way to loop over the Napi::Array is to maintain the index manually and loop over using traditional for loops:

    auto napiArray = Napi::Array::New(info.Env());
	for (uint32_t i = 0, len = cppVector.size(); i < len; i++) {
        napiArray[i] = Napi::Number::New(info.Env(), cppVector[i]);
    }

However, I'd like to be able to use ranged for loop or STL algorithms:

    auto napiArray = Napi::Array::New(info.Env());
    std::transform(cppVector.begin(), cppVector.end(), napiArray.begin(), 
	[&info](auto a) { return Napi::Number::New(info.Env(), a); });

or other similar examples

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions