Giter VIP home page Giter VIP logo

Comments (5)

sagacity avatar sagacity commented on August 19, 2024

The result is kind of unexpected. It is by design that the mapping plugin cannot handle arrays with duplicate objects (see the key callback part of the mapping plugin documentation).

For an array of primitives like you have here, I guess duplicates should be allowed.

Is your use-case with an array of primitives like in your example, or are you using objects?

from knockout.mapping.

mrDarcyMurphy avatar mrDarcyMurphy commented on August 19, 2024

The example I posted is a simplification but yeah, it matches my use case almost exactly. Here's a closer example:

data = {
  ...
  properties: {
    arrayKey: ["1", "1", "1", "2"]
  }
}

I'm grabbing a bunch of data via api, and the array that I noticed this happening on is one of a handful of properties that's stored in a serialized object. Until now we've been able to infer what we need from the length of the array and it's contents. It should be possible to rework things on the back-end and add a key, but being able to bind the existing array would be much easier for now.

from knockout.mapping.

sagacity avatar sagacity commented on August 19, 2024

Ok! Well, if you have the time to create a pull request that would be highly appreciated :) Otherwise it may take a few days before I can get around to fixing it.

from knockout.mapping.

mrDarcyMurphy avatar mrDarcyMurphy commented on August 19, 2024

Alright. I'll see what I can do.

from knockout.mapping.

mrDarcyMurphy avatar mrDarcyMurphy commented on August 19, 2024

I haven't had a lot of time to look at this, but I've found that the culprit is line 305:

var index = ko.utils.arrayIndexOf(ko.utils.unwrapObservable(rootObject), item);

What happens is ko.utils.arrayIndexOf() returns the first matching index, which means that when the next line fires...

newContents[index] = mappedItem

...the same index gets used, so that item in the newContents array gets written over again and again. But since the mapping plugin is using ko.utils.arrayIndexOf I'm not sure how to proceed.

from knockout.mapping.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.