<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Серый форум &mdash; AHK: Объединить объекты]]></title>
	<link rel="self" href="https://forum.script-coding.com/extern.php?action=feed&amp;tid=17235&amp;type=atom" />
	<updated>2022-07-07T22:46:07Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.script-coding.com/viewtopic.php?id=17235</id>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Объединить объекты]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153822#p153822" />
			<content type="html"><![CDATA[<p>Хм.. Возможно я перепутал, сейчас смотрел код класса для работы с массивами и нашел там метод &quot;shift&quot; который реализовали как:<br /></p><div class="codebox"><pre><code>shift(arr)
{
	return arr.RemoveAt(1)
}</code></pre></div><p>Значит и мой код можно сократить до:<br /></p><div class="codebox"><pre><code>source := sources.removeAt(1)</code></pre></div>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-07T22:46:07Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153822#p153822</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Объединить объекты]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153821#p153821" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Phoenixxx_Czar пишет:</cite><blockquote><p>этот (shift) метод есть у массивов в AHK</p></blockquote></div><p>Где вы его обнаружили? Я о таком ничего не знаю.</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-07T22:41:23Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153821#p153821</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Объединить объекты]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153820#p153820" />
			<content type="html"><![CDATA[<div class="quotebox"><cite>Phoenixxx_Czar пишет:</cite><blockquote><p>source := sources[1]<br />sources.removeAt(1)</p></blockquote></div><p>Вместо этого кода в решении на JS используется &quot;.shift()&quot;, насколько я помню он работает так же, как я написал это другим кодом и насколько я знаю, этот (shift) метод есть у массивов в AHK. Но он почему-то не срабатывает в AHK.<br />Вопрос уже решен добавлением метода &quot;assignDeep&quot;.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-07T22:35:46Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153820#p153820</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Объединить объекты]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153819#p153819" />
			<content type="html"><![CDATA[<p>Я сейчас даже не понял, в чём вопрос. О каком shift() идёт речь, и где он не срабатывает?</p>]]></content>
			<author>
				<name><![CDATA[teadrinker]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=24515</uri>
			</author>
			<updated>2022-07-07T22:23:09Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153819#p153819</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: AHK: Объединить объекты]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153818#p153818" />
			<content type="html"><![CDATA[<p>Пришлось переписать JS код взятый из <a href="https://stackoverflow.com/a/34749873/15606261">поста</a>.</p><div class="codebox"><pre><code>class Object
{
	assign(target, sources*)
	{
		target := Object.clone(target)

		for i, arr in sources
		{
			for key, value in arr
			{
				target[key] := (IsObject(value) &amp;&amp; !value.base.IsArray) ? Object.clone(value, target[key]) : value
			}
		}

		return target
	}

	assignDeep(target, sources*)
	{
		if (!sources.count())
		{
			return target
		}

		source := sources[1]
		sources.removeAt(1)

		if (isObject(target) &amp;&amp; isObject(source))
		{
			for key in source
			{
				if (isObject(source[key]))
				{
					if (!target[key])
					{
						target[key] := {}
					}

					Object.assignDeep(target[key], source[key])
				}
				else
				{
					target[key] := source[key]
				}
			}
		}

		return Object.assignDeep(target, sources*)
	}

	clone(obj)
	{
		cloned := obj.clone()

		for i, key in obj
		{
			if IsObject(key)
			{
				cloned[i] := Object.clone(key)
			}
		}

		return cloned
	}
}
</code></pre></div><p>Мне пришлось использовать такую конструкцию:<br /></p><div class="codebox"><pre><code>source := sources[1]
sources.removeAt(1)</code></pre></div><p>... дабы получить и удалить первый элемент массива, почему не срабатывает <em>.shift()</em>? Метод уходит в бесконечную рекурсию ибо <em>.shift()</em> ничего не возвращает.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-07T21:05:54Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153818#p153818</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[AHK: Объединить объекты]]></title>
			<link rel="alternate" href="https://forum.script-coding.com/viewtopic.php?pid=153817#p153817" />
			<content type="html"><![CDATA[<p>Я пытался реализовать <em>Object.assing</em> из JS, раньше не особо заморачивался, но сейчас хочу решить одну проблему, я не получаю нужный результат:<br /></p><div class="codebox"><pre><code>class Object
{
	assign(target, sources*)
	{
		target := Object.clone(target)

		for i, arr in sources
		{
			for key, value in arr
			{
				target[key] := (IsObject(value) &amp;&amp; !value.base.IsArray) ? Object.clone(value, target[key]) : value
			}
		}

		return target
	}

	clone(obj)
	{
		cloned := obj.clone()

		for i, key in obj
		{
			if IsObject(key)
			{
				cloned[i] := Object.clone(key)
			}
		}

		return cloned
	}
}
</code></pre></div><div class="codebox"><pre><code>
obj1 := {a: {a: 1}}
obj2 := {a: {b: 2}}
obj3 := {b: 1}

test := Object.assign(obj1, obj2, obj3) ; {&quot;a&quot;: {&quot;b&quot;: 2}, &quot;b&quot;: 1}
; Ожидаемым результатом было: {&quot;a&quot;: {&quot;a&quot;: 1, &quot;b&quot;: 2}, &quot;b&quot;: 1}
</code></pre></div><p>Хотя.. Данный код обрабатывается точно так же и в JS, получается мне нужно что-то типо deepAssign.</p>]]></content>
			<author>
				<name><![CDATA[Phoenixxx_Czar]]></name>
				<uri>https://forum.script-coding.com/profile.php?id=34426</uri>
			</author>
			<updated>2022-07-07T20:32:01Z</updated>
			<id>https://forum.script-coding.com/viewtopic.php?pid=153817#p153817</id>
		</entry>
</feed>
