Add property to javascript array

Arrays have a "length" property by default.

Can I add custom properties to them?

Without having to make them objects

Score: 51
Tags: javascript, arrays
Views: 49332
Date posted: 3/31/2012

Add JavaScript object to JavaScript object

I'd like to have JavaScript objects within another JavaScript object as such:

Issues:

  - {"ID" : "1", "Name" : "Missing Documentation", "Notes" : "Issue1 Notes"}
  - {"ID" : "2", "Name" : "Software Bug", "Notes" : "Issue2 Notes, blah, blah"}
  - {"ID" : "2", "Name" : "System Not Ready", "Notes" : "Issue3 Notes, etc"}
  // etc...

So, I'd like "Issues" to hold each of these JavaScript objects, so that I can just say Issues[0].Name, or Issues[2].ID, etc.

I've created the outer Issues JavaScript object:

var jsonIssues = {};

I'm to the point where I need to add a JavaScript object to it, but don't know how. I'd like to be able to say:

Issues<code here>.Name = "Missing Documentation";
Issues<code here>.ID = "1";
Issues<code here>.Notes = "Notes, notes notes";

Is there any way to do this? Thanks.

UPDATE: Per answers given, declared an array, and am pushing JavaScript objects on as needed:

var jsonArray_Issues = new Array();

jsonArray_Issues.push( { "ID" : id, "Name" : name, "Notes" : notes } );

Thanks for the responses.

Score: 45
Tags: javascript, object, add
Views: 162706
Date posted: 11/17/2009

Javascript add item to current array

I am trying to add an item to a current array.

var arrayValues = new Array();
arrayValues.push("Value 1");
arrayValues.push("Value 2");
arrayValues = document.getElementsByTagName('a');
arrayValues.push("Value 3");

By doing this way I get a error, and I dont get value 1 and value 2, after getting the hyperlink collection when I try to add a new item it throws Error: Object doesn't support this property or method which is the push method.

What is happening to the array after the collection of hyperlinks is assigned ? How can I add a new item to it ?

Score: 11
Tags: javascript, arrays, array-push
Views: 28474
Date posted: 3/8/2011

Add element to array associative arrays

All examples of adding new elements to associative arrays are going the "easy" way and just have a one dimensional array - my problem of understanding is having arrays within arrays (or is it objects in arrays?).

I have the following array:

var test = [
            {
                value: "FirstVal",
                label: "My Label 1"
            },
            {
                value: "SecondVal",
                label: "My Label 2"
            }
           ];

Two questions: How to generate this array of associative arrays (yes... object) from scratch? How to add a new element to an existing array?

Thanks for helping me understand javascript.

Score: 9
Tags: javascript, associative-array
Views: 45502
Date posted: 12/19/2011

Can node-webkit C++ addon write to ImageData object created in JavaScript context?

I'm writing a node-webkit addon in C++ that decodes image data and returns an array of pixel data to the caller. The following code (abridged for brevity) works just fine:

caller.js

var image_data = canvas_context.createImageData(w, h);
var addon = require('decoder.node');
var decoder = new addon.Decoder();
var pixel_array = decoder.getPixelArray();
image_data.data.set(pixel_array);
canvas_context.putImageData(image_data);

decoder.cpp

Handle<Value> Decoder::getPixelArray(const Arguments &args) {
    HandleScope scope;

    // unwrap self
    DecoderObj *obj = ObjectWrap::Unwrap<DecoderObj>(args.This());
    if (obj == NULL) return scope.Close(Number::New(1));

    // get pointer to pixel data
    PixelPtr *ptr = NULL;
    uint64_t len = 0;
    obj->GetPixelsPtr((PixelPtr*)&ptr, &len);

    // create javascript native array
    // from http://luismreis.github.io/node-bindings-guide/docs/returning.html
    v8::Handle<Value> fun_val = Context::GetCurrent()->Global()->Get(String::New("Uint8ClampedArray"));
    v8::Handle<Function> fun = v8::Handle<Function>::Cast(fun_val);
    const unsigned argc = 1;
    Local<Value> argv[argc] = { Local<Value>::New(Uint32::New(len)) };
    Local<v8::Object> array = fun->NewInstance(argc, argv);

    // populate js native array with pixel ptr data
    array->SetIndexedPropertiesToExternalArrayData(ptr, v8::kExternalUnsignedByteArray, len);

    // return native array to javascript caller
    return scope.Close(array);
}

The only problem is when the caller invokes the set() method on the ImageData object. This has proven to be unacceptably slow (i.e. >= 20ms). I assume the slowness is due to a large amount of data being copied from one memory location to another. So I thought I'd try to fix this by passing a reference to the ImageData object into the addon, and then have the addon write directly to the object, thereby eliminating the need for a memory copy. Unfortunately I haven't been able to get this to work. Here's the modified code that doesn't work:

caller.js

var image_data = canvas_context.createImageData(w, h);
var addon = require('decoder.node');
var decoder = new addon.Decoder();
decoder.getPixelArray(image_data.data);
canvas_context.putImageData(image_data);

decoder.cpp

Handle<Value> Decoder::getPixelArray(const Arguments &args) {
    HandleScope scope;

    // unwrap self
    DecoderObj *obj = ObjectWrap::Unwrap<DecoderObj>(args.This());
    if (obj == NULL) return scope.Close(Number::New(1));

    // convert image_data arg into local array
    Local<Array> array = Array::Cast(*args[0]);

    // get pointer to pixel data
    PixelPtr *ptr = NULL;
    uint64_t len = 0;
    obj->GetPixelsPtr((PixelPtr*)&ptr, &len);

    // populate image_data array with pixel ptr data
    array->SetIndexedPropertiesToExternalArrayData(ptr, v8::kExternalUnsignedByteArray, len);
}

I know this doesn't work because putImageData() results in a black frame, whereas the original code successfully draws the actual frame as expected.

Here is my question: is it possible to create an ImageData object and/or Uint8ClampedArray in JavaScript, pass it by reference into a Node.js addon, have the addon write to the object's memory location, and then be able to use the object in the JavaScript context without performing a memory copy??

I don't know if I'm doing it wrong, or it's impossible, or both. :)

Score: 6
Tags: javascript, c++, node.js, node-webkit, add-on
Views: 524
Date posted: 1/29/2015


1


© 2021 Search Overflow
Contributions licensed under cc by-sa